Call to OpenFileDialog or SaveFileDialog hangs or freezes

Today I was debugging a very strange issue when call to OpenFileDialog and SaveFileDialog hanged, froze, never returned…

At first I thought the problem is, that call is made from SysTray (Notification area) hidden window. However, digging deeper things got more bizarre. I found a thread in MS forum, where was a workaround – add help button to OpenFileDialog.

When I say that it “hangs” I mean that the dialog does not display but no exception is thrown and the system eventually throws in a little wait cursor to indicate that the program is getting time. It doesn’t hang the entire computer, fortunately, but the application has to be terminated.

For OpenFileDialog the ShowHelp property must be explicitly set.
For SaveFileDialog the ShowHelp, CreatePrompt, and OverwritePrompt properties must be explicitly set.

It’s not consistent though. I’ve had SaveFileDialog work with just ShowHelp set in simple situations. But I have an app which didn’t start running again until I also set CreatePrompt and OverwritePrompt properties then it started working again. They don’t have to be set to true, they just have to be initialized to either true or false.

Original forum post: click here.

So I tried:
myDialog.ShowHelp = true;

And it worked. So bizarre I thought.

After little more testing I found next problem. If file is not found, then dialog must show warning that ‘File not found. Check the file name and try again.’ And it showed the warning… and hanged…

From the beginning I was ignoring one fact, that this actually is a .NET COM object, and that I am calling it from MTA threading model (multi-thread apartment). By default .NET COM objects advertise themselves as BOTH (STA, MTA), so I changed calling thread to STA. Changed CoInitializeEx to CoInitialize, and suddenly everything worked like a charm.

I am not a COM expert, so if you find some flaws please let me know in comments!

16 replies on “Call to OpenFileDialog or SaveFileDialog hangs or freezes”

  1. Thanks a lot.. i was struck up with this problem (openfiledialog.showdialog() issue) and ur blog saved a lot of time and i was able to fix it simply by setting showhelp property.. Thanks a ton again….

    PS: any idea on why we have to set this property. Because i was calling this function until a week back and application works perfectly.. but suddenly today i got this error message and the entire application freezes. ????

  2. I encountered this problem, but my case is in MFC.
    e.g. I make new MFC application project. and then start debugging,
    I selected MENU-file-open, and selected a file.
    I terminated my application.
    I start myapplication again.
    I selected MENU-file-open, and selected a file.
    I terminated my application. 2nd time.
    I start myapplication again. 3rd time.
    … 5th time, open-file and Freeze all Window, but not Freeze Windows 7.
    only mouse is working.
    I had to push the reset button of my PC.

  3. This is another ridiculous .NET peculiarity.
    You shouldn’t have to deal with Threading and COM details when all you want is to create a single file selection dialogue.

    Thanks for the solution!

  4. Thank you for the solution – use of STA for my GUI thread. I would not have tracked that down for a long time.

  5. I am a beginner in vb .
    I am facing the same problem.
    could you please tell me how to change calling thread to STA. how to Change CoInitializeEx to CoInitialize .
    thanks in advance.

  6. I don’t have create and overwritePrompt in OpenFileDialog, but just setting ShowHelp to true solved my problem, so thank you for saving my day!

    Cheers

  7. Woooow! What in the world?! I’m so glad I found this!

    My app was working fine until recently. Then, suddenly, the Dialog would hang for 6+ seconds after selecting a file!

    Added the ShowHelp switch and like magic it’s back to normal.

    I’m really loving .NET, but there are some really strange bugs occasionally.

  8. O MY! Luckily I found this in minutes after my application started to freeze on openfiledialog. Show help solved it right away ! Strange ! :) Thank you so much !

  9. Upgraded to vs 2017 with .Net 4.6 and my application would not show the file save dialog box as mentioned above. I added the setting of ShowHelp to true and it worked. Microsoft WTF.

  10. Note on my previous comment. It would only not show the dialog after 8 working attempts if done in the proper order. Why I don’t know, but I was only able to reproduce the bug if I followed exact same steps each time.

Comments are closed.