4

My WPF application consists of a main window, which in turn has a child window.

If I let the child window open a FolderBrowserDialog, I get a System.ComponentModel.Win32Exception (A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in WindowsBase.dll). More specifically this occurs after closing the dialog, and when the child window gets focus again (?). The question mark is there because I haven't succeeded in catching the exception so I'm not 100% sure. But visually, that is when it occurs. If the child window has focus after closing the dialog, the exception occurs after the window looses focus and the regains it.

If I move the code opening the folderBrowserDialog to the main window, this does not happen. ***EDIT: Now it also crashes the program from the main window too.

Why do I get this exception? What is happening? How can I fix it (apart from moving it to the main window)?

Here is the code opening the dialog; nothing extraordinary. Exception code removed.

 FolderBrowserDialog dlg = new FolderBrowserDialog();          
 DialogResult result = dlg.ShowDialog();

 if (result == DialogResult.OK)
    selDir = dlg.SelectedPath;

I'm using VS2010, .Net 4.

AnnaR
  • 3,166
  • 6
  • 35
  • 39
  • 1
    possible duplicate of [FolderBrowserDialog - Win32Exception occurred - The parameter is incorrect](http://stackoverflow.com/questions/3438985/folderbrowserdialog-win32exception-occurred-the-parameter-is-incorrect) – Hans Passant Apr 03 '12 at 12:22

1 Answers1

5

The bit about "first chance exception" sounds like you're seeing this in the debugger, and also sounds like you've enabled "Break on thrown exceptions" in Debug > Exceptions. With that setting, you'll break into the debugger as soon as an exception is thrown, even if there's already a catch block for it somewhere up the call stack (i.e., even if it wouldn't actually crash your program). It sounds like you're seeing an exception that's getting thrown and then immediately caught by the Framework -- so from your point of view, it's a false positive.

Try running your program without the debugger (the "Run without debugging" menu item, or just double-click your EXE in Explorer) and see if the program actually crashes. If it doesn't, then you know it's a false positive -- at which point, you might want to go into Debug > Exceptions and uncheck the boxes in the "Thrown" column, so you stop getting bugged by this sort of thing. (The "Thrown" column is unchecked by default for this reason.)

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • Great idea! I haven't thought about trying that. Test-result: Unfortunately the program freezes when run without debugging. Really really irritating. I was so hoping that unchecking some exceptions would do it. – AnnaR Apr 05 '12 at 07:26
  • I have the same problem only on debugging mode, and the application works fine when running without debugging. but even when I've unchecked all the boxes, I still can't run it on debugging mode. and getting the same exception. do you have any suggestions? – IBRA Jun 15 '16 at 11:39