Click here to Skip to main content
15,354,816 members
Articles / Programming Languages / C#
Article
Posted 27 Mar 2006

Stats

117.1K views
209 downloads
43 bookmarked

How to avoid WSODs in the Visual Studio 2005 Designer

Rate me:
Please Sign up or sign in to vote.
4.75/5 (22 votes)
3 Apr 20063 min read
An article on how to deal with designer exceptions in Visual Studio 2005.

Sample Image - 01 article.jpg

Introduction

This article explains how to tackle one of the most dreadful situations Windows Forms developers encounter during their daily work - WSODs.

Background

The term WSOD (White Screen of Darn) was set by Microsoft employees in an MSDN blog.

Using the code

Imagine the following scenario. One morning you arrive at your workplace, turn your monitor on, and then try to open a form in your application that builds and runs perfectly well, and instead of seeing your handcrafted form, you see a big frightening screen with the following title in red, and a big X near it:

One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.

As a pretty fresh GUI developer, but a pretty experienced software developer all-in-all, I was pretty astonished when I first encountered this phenomenon. My coworkers were not as surprised as I was, but still solving this sort of a problem never seemed to be trivial.

Explaining the problem

When the Visual Studio Designer views forms, it runs the constructor of your code. However, the code may behave in an unexpected way when run out of the application context. This may result in some exceptions thrown. The Visual Studio Designer does not like those exceptions, and may show a screen similar to this one, instead of the screen you really wanted to see:

Image 2

About the example

My example code consists of a form called MainForm. This form contains a user control called SevenHolder. The SevenHolder user control has nothing but a text box, whose content is updated from a singleton. The singleton class holding this information, simply called Singleton, holds an integer, which seems always to be 7. However, another singleton class called Global, limits the creation of the Singleton instance only to the time when the application is running. The Global instance is created as the first thing in this program, in Program.cs, where its Running attribute is set to true. Since SevenHolder updates the text box in the constructor, but the constructor will only obtain the Singleton instance on runtime, an exception will be thrown when the code is run in design-time.

The solution

Finding this sort of problems may prove very difficult by only reviewing the code. However, an easy solution exists. By taking the following steps, you may find the exact location of the code causing the exception, thus making it possible to change it in a way that the Designer works well with it:

  1. Open a new Visual Studio instance.
  2. Open any source file. This is required so that Visual Studio lets you attach to a process.
  3. Attach the new Visual Studio instance to the first one. The Visual Studio process is called devenv.exe. You only need to attach to managed code. Set Visual Studio to break on Common Language Runtime exceptions - Thrown and User-handled.
  4. Close the problematic form, and reopen it.
  5. This should result in an exception caught inside your code in the second instance of Visual Studio!

    Image 3

    In our case, the following code was causing the problem:

    C#
    this.labelSevenHolder.Text = sing.Information.ToString();

    In order to fix it, two approaches may be taken. The first one is to check the value of sing before using it:

    C#
    public SevenHolder()
    {
        InitializeComponent();
    
        Singleton sing = Singleton.Instance();
    
        if (sing != null)
        {
            this.labelSevenHolder.Text = sing.Information.ToString();
        }
    }

    The second approach is to check the value of the DesignMode property inherited from System.ComponentModel.Component to see whether the application is indeed running before doing anything useful. This check should always be performed after the call to InitializeComponent though, otherwise your control may not render correctly in design-time:

    C#
    public SevenHolder()
    {
        InitializeComponent();
    
        if (this.DesignMode)
            return;
    
        Singleton sing = Singleton.Instance();
    
        this.labelSevenHolder.Text = sing.Information.ToString();
    }

    And of course, the complete code should look like this:

    C#
    public SevenHolder()
    {
        InitializeComponent();
    
        if (this.DesignMode)
            return;
    
        Singleton sing = Singleton.Instance();
    
        if (sing != null)
        {
            this.labelSevenHolder.Text = sing.Information.ToString();
        }
    }
  6. After recompiling the code, reopening MainForm should result with the form. In this case, it does not look exactly in design-time as the form in run-time, because, of course, the content of the label inside the user control is only known to the application in run-time.

    Image 4

Points of Interest

Investigating this sort of problems really makes one understand how the Visual Studio designer works under-the-hood.

History

  • Minor fixes: 31/3/2006.
  • Initial version: 28/3/2006.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

nadav74
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralI don't even get a WSOD anymore... Pin
primem0ver14-Mar-11 18:04
Memberprimem0ver14-Mar-11 18:04 
I don't even get a WSOD. Well... it is white. But all I have seen since reinstalling Visual Studio 2005 (fresh) on my fresh install of Windows 7 is a single line of HTML code. (AND YES... I did download the Service Packs and Vista/7 updates!). It doesn't even seem to be reading it as a web page but as simple text. I spent 4-5 hours fixing a problem with a form because I forgot that the WSOD actually does give a clue the problem at least. But I didn't even see the error amongst all the HTML formatting code.

In an attempt to avoid another 4-5 hour of complete frustration I read the line very carefully and suddenly remembered that the HTML is supposed to be rendered as the WSOD and tell you something about the problem. So I searched online for a way to get the WSOD to display correctly to no avail. But I did find this article. Does anyone know how to get Visual studio render the WSOD page correctly?

Honestly... I hate VS 2005. Every time I install it I get a different bug where it is supposed to do something and doesn't. Why? The particular issue worked right the last time I installed it!!!

Meanwhile... I guess I will follow this procedure to find the problem. Thanks!
GeneralSuggestion: Pin
sherifffruitfly24-May-09 8:57
Membersherifffruitfly24-May-09 8:57 
GeneralMore info on this Pin
tonyt10-Dec-08 6:27
Membertonyt10-Dec-08 6:27 
GeneralWorks great for WPF too! Pin
Kenrae4-Nov-08 21:59
MemberKenrae4-Nov-08 21:59 
GeneralRe: Works great for WPF too! Pin
nadav744-Nov-08 22:02
Membernadav744-Nov-08 22:02 
GeneralDesigner View problem Pin
ramki_mars15-May-08 18:28
Memberramki_mars15-May-08 18:28 
GeneralProblem in viewing designer of windows form c# Pin
ramki_mars11-Apr-08 1:17
Memberramki_mars11-Apr-08 1:17 
GeneralAnother simple way to debug this type of error Pin
Natza Mitzi23-Dec-07 1:45
MemberNatza Mitzi23-Dec-07 1:45 
GeneralRe: Another simple way to debug this type of error Pin
SerialHobbyist8-Mar-09 1:27
MemberSerialHobbyist8-Mar-09 1:27 
Generalthis.DesignMode Pin
Jakub Mller14-Aug-07 21:19
MemberJakub Mller14-Aug-07 21:19 
GeneralRe: this.DesignMode Pin
deerchao2-Nov-07 17:18
Memberdeerchao2-Nov-07 17:18 
GeneralGood article Pin
Christian Graus31-Jan-07 9:24
mveChristian Graus31-Jan-07 9:24 
GeneralRe: Good article Pin
nadav7431-Jan-07 9:32
Membernadav7431-Jan-07 9:32 
QuestionRe: Good article Pin
leoprex5-Feb-07 6:36
Memberleoprex5-Feb-07 6:36 
AnswerRe: Good article Pin
Christian Graus5-Feb-07 8:57
mveChristian Graus5-Feb-07 8:57 
GeneralRe: Good article Pin
leoprex5-Feb-07 10:05
Memberleoprex5-Feb-07 10:05 
Generalactivex as a singleton?! Pin
lnae15-Nov-06 1:43
Memberlnae15-Nov-06 1:43 
GeneralNo menu entry 'Exceptions' Pin
Wilhelm Berg2-Aug-06 20:57
MemberWilhelm Berg2-Aug-06 20:57 
JokeWSOD Pin
robbyt4-Apr-06 1:24
Memberrobbyt4-Apr-06 1:24 
JokeRe: WSOD Pin
nadav745-Apr-06 0:24
Membernadav745-Apr-06 0:24 
GeneralExceptions in UserControls Pin
DanielFaensen31-Mar-06 3:29
MemberDanielFaensen31-Mar-06 3:29 
GeneralRe: Exceptions in UserControls Pin
tonyt4-Apr-06 13:29
Membertonyt4-Apr-06 13:29 
GeneralRe: Exceptions in UserControls Pin
DanielFaensen5-Apr-06 4:23
MemberDanielFaensen5-Apr-06 4:23 
Questionif ( DesignMode ) ? Pin
Corneliu Tusnea27-Mar-06 20:27
MemberCorneliu Tusnea27-Mar-06 20:27 
AnswerRe: if ( DesignMode ) ? Pin
nadav7427-Mar-06 20:35
Membernadav7427-Mar-06 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.