Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Prevent code executing at Design Time in Visual Studio for Winforms Apps

0.00/5 (No votes)
28 Apr 2010 1  
If you have a Winforms application that auto loads data, there is no doubt that you’ll have come across the problem of data loading at design time (i.e. when opening the code in the designer). At best, this slows the designer down, at worst it might crash VS and prevent the control from...
If you have a Winforms application that auto loads data, there is no doubt that you’ll have come across the problem of data loading at design time (i.e. when opening the code in the designer). At best, this slows the designer down, at worst it might crash VS and prevent the control from rendering. Typically, I have used this as a test:

if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
//...Then Continue


This tests to see if the code is being executed under the account Visual Studio uses. This has the potential to be flaky for various reasons, I have discovered a better way to achieve what I originally wanted:

if(System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
//... Then Continue

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