Jun 10, 2009

How to avoid situations where Visual Studio Designer crashes because of code running in Design Mode

The VS Designer will try to create your UI and will call the control constructor. If you have code that connects to a DB for instance in the constructor, the Designer will crash. To avoid that, a simple check [ if (!DesignMode) { ...get DB connection...} ] using the following property will do the trick:


/// <summary>
/// Verificam daca procesul care ruleaza codul este VisualStudio si ignoram ca sa nu ne crape in Designerul VS.
/// </summary>
public static bool DesignMode
{
get { return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv"); }
}


Method Nr. 2:

using System.ComponentModel;

public static bool DesignModeLicUsage
{
get
{
return (LicenseUsageMode.Designtime == LicenseUsageMode.Designtime);
}
}

No comments:

Post a Comment