Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

Archive for July 2007

Wanna Crash Visual Studio?

with one comment

I believe that I found an interesting bug in Visual Studio. I was designing a progress dialog for use in our application. I wanted the dialog to be threaded, so the user could cancel the operation or continue to work in our application.

Since there were going to be a handful of “operations” that would require the progress dialog, I was going to encapsulate most of the functionality in a base form. Each operation (which may or may not require user input) would then have it’s own derived form.

NOTE: I’ve put together a sample project[^] that reproduces the issue so you can follow along if you like.

If you open the sample project in VS and run it, you will see the following window:

If you click the execute button, then you will get the following confirmation dialog (which is presented by the progress dialog):

If you click no, then the progress dialog is dismissed and the “operation” is not executed. If you click yes, then the operation will be performed and you will see the following:

So, the code works great, but doesn’t work so well in the VS designer. The progress dialog is made up of two forms: BaseDialog and DerivedDialog. The former provides the bulk of the functionality and the latter does the actual “operation”.

If you open BaseDialog in the VS designer, then all is well. If you try to open DerivedDialog in the VS designer, then you will see the same “Confirm” message box you see during execution. After you click the “Yes” or “No” button, then VS will crash.

Keep in mind that all of the code that shows the message box is located in BaseDialog. So this means that VS must be suppressing this code when the BaseDialog is loaded in the designer, but is not suppressing it when derived dialogs are loaded in the designer.

We can fix this by wrapping the offending code in an “if” statement that verifies that the DesignMode[^] property of the form is false. This means that the code will not be executed if the form is being loaded in the designer.

Written by Tom

July 4, 2007 at 10:36 am

Posted in Gotcha, Visual Studio