In my WPF application which uses the Caliburn.Micro and MahApps.Metro frameworks, i open instances of OpenFileDialog from the System.Windows.Forms namespace. After opening and closing this dialog, the rest of my application hangs for approximately 3 to 4 seconds. This is also true when using the Microsoft.Win32 namespace.
I've done some research myself and come up with a handful of posts complaining of the same issue, and the solutions they found was two fold:
- Quick fix: set the
ShowHelpproperty totrue - Proper fix: Ensure it's running in
Single Threaded Apartmentmode.
The ShowHelp fix works for me by using a non-vista style dialog, but from an UX perspective on Windows Vista and newer, it doesn't look right. The other thing for me then would be to ensure it's in STA mode.
WPF uses STA by default, but I tried writing my own main method and explicitly stating [STA] but had no luck. I added the below snippet to my code, but the output is State: STA, indicating there should be no issue.
Console.WriteLine("State: {0}", System.Threading.Thread.CurrentThread.GetApartmentState());
var dialog = new OpenFileDialog
I've also tried launching the OpenFileDialog on a new STA thread, and while it launches and doesn't hang my application, it also isn't modal anymore. I've also tried joining the new thread, but it still hangs after closing.
Question
Is there a known, underlying issue with OpenFileDialogand if so, are there alternate ways of launching the FileDialogs that may avoid this hanging?
Sources for the ShowHelp and STA comments: