5

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:

  1. Quick fix: set the ShowHelp property to true
  2. Proper fix: Ensure it's running in Single Threaded Apartment mode.

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:

Community
  • 1
  • 1
BlackBox
  • 2,153
  • 1
  • 18
  • 35
  • 1
    OpenFileDialog injects a very large amount of code into a process. Shell extensions, programmers tend to have a lot of them installed. Not always of the best quality. Use SysInternals' AutoRuns utility to narrow down to the troublemaker, ask questions about it at superuser.com – Hans Passant Jan 27 '17 at 12:35
  • OpenFileDialog is a Winforms class. The 'correct' way to have such a functionality is to implement some kind of IOpenFileDialogService, MVVM style. – Kilazur Jan 27 '17 at 12:44
  • You could try to use the Microsoft.Win32.OpenFileDialog: http://stackoverflow.com/questions/10315188/open-file-dialog-and-select-a-file-using-wpf-controls-and-c-sharp – mm8 Jan 27 '17 at 12:46
  • I've also tried the `Win32` namespace, and it's the same issue – BlackBox Jan 27 '17 at 12:58

0 Answers0