-3

I have a winforms app, and in my app, i have a custom form I created. This custom from has the default close, minimize, and maximize buttons in the top right corner. If I close the form using the default red x button, it closes fine with no problems. However, if I try to close the form by calling .Close() on the form, it throws an unhandled InvalidOperationException exception.

What is the difference between calling .Close(), and clicking that button? Why does clicking the button work fine, but calling .Close() throw the exception?

Tyler Jones
  • 1,283
  • 4
  • 18
  • 38
  • 1
    When you receive an exception you could easily see the stack trace. This info is fundamental to understand what is causing the exception. You could add that info here – Steve Aug 17 '15 at 18:09
  • @Steve there is no stack trace. The application doesn't crash or throw the exception. The only way I know it is happening is because the output shows the line: "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" – Tyler Jones Aug 17 '15 at 18:20
  • @Steve also, i know it's happening because the form doesn't close. – Tyler Jones Aug 17 '15 at 18:22
  • @TylerJones It's possible you are suppressing the exception then. Are there any try/catch clauses that you could comment out during your testing? It may be necessary to show some of your code... – Nathan A Aug 17 '15 at 18:23
  • @NathanA no, there are no try/catches surrounding any of the code in question. – Tyler Jones Aug 17 '15 at 18:25
  • where do you call `this.Close();`. on the mainthread? please show the part you are calling the method. – M.kazem Akhgary Aug 17 '15 at 18:41
  • public void WebBrowserCallback() { if (_cefBrowser != null) { _cefBrowser.Close(); } } – Tyler Jones Aug 17 '15 at 20:19
  • .Hide() throws the exact same error. – Tyler Jones Aug 17 '15 at 20:19
  • Why are people voting to close this question? I need some help here. This is a programming question. Isn't stackoverflow for programming questions? This is not a networking question or english language question. – Tyler Jones Aug 17 '15 at 22:24

1 Answers1

1

You can use TOOLS->Spy++ to log the window messages.

From: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,b6605a5b113b45f7

The Form.Close() method calls:

closeReason = CloseReason.UserClosing;
SendMessage(NativeMethods.WM_CLOSE, 0, 0);

Also, try searching for the exception message: https://www.google.com/search?q=Form+C%23+A+first+chance+exception+of+type++webbrowser&ie=utf-8&oe=utf-8

Came up with this links:

C# : "A first chance exception of type 'System.InvalidOperationException'"

Community
  • 1
  • 1
Loathing
  • 5,109
  • 3
  • 24
  • 35