Click here to Skip to main content
Click here to Skip to main content

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

, 28 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Keith Barrow
Software Developer (Senior)
United Kingdom United Kingdom
I Graduated in Natural Sciences (Chemistry & Physics) from Durham University, where I did not cover myself with glory, but did fill, and often cover, myself with beer.
 
I qualified as a secondary school teacher, but I hated teaching. I worked as an IT techhie for a few years but kept being promoted to IT Trainer, thanks to my teaching experience, which I also disliked. I spent some time working out what else I could do instead and reduced beer intake.
 
I realised that I should work as a programmer, having enjoyed it a hobby since I was a nipper in the halcyon days of the Sinclair Spectrum (48k, Rubber Keyboard). Spent two weeks working out why I didn't think of this to start with, instead of starting my dull-as-ditch-water Chemistry degree 8 years earlier. Had a beer to celebrate.
 
I Graduated in 2001 with an MSc from Newcastle Uni in Comp Sci. Did cover myself with glory, and drank some beer.
 
.Netting ever since, and loving it. Though I have largely given up beer due to not being able to hack the pace like I used to.
 
I was born, brought up, and have lived most of my life near Newcastle. In a fit of temporary insanity I moved to Amman, in my wife's homeland of Jordan, but made it back safely to the UK without any extra holes being made in my person by bullets. To be fair it was pretty safe at the time, if you ignored the roads.
 
Visit Jordan if you can by the way, the landscape is beautiful and varied, the food excellent and the people the friendliest on earth, after Geordies naturally Smile | :) .
Follow on   Twitter   Google+

Comments and Discussions

 
Suggestionwhy so complicate? PinmemberAdam Wyżgoł18-Dec-12 4:30 
GeneralRe: why so complicate? PinmentorKeith Barrow18-Dec-12 4:49 
GeneralMy vote of 5 PinmemberAlberto Richart28-Jun-12 2:58 
GeneralMy vote of 5 Pinmemberh3y26-Jun-12 4:48 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 28 Apr 2010
Article Copyright 2010 by Keith Barrow
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid