Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
28 Apr 2010CPOL 43.8K   8   7
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)


Written By
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 | :) .

Comments and Discussions

 
QuestionWindows form Pin
FELIX 102206912-Apr-15 18:23
FELIX 102206912-Apr-15 18:23 
AnswerRe: Windows form Pin
Keith Barrow3-Apr-15 12:42
professionalKeith Barrow3-Apr-15 12:42 
QuestionRe: Windows form Pin
FELIX 102206913-Apr-15 18:13
FELIX 102206913-Apr-15 18:13 
Suggestionwhy so complicate? Pin
Adam Wyżgoł18-Dec-12 4:30
professionalAdam Wyżgoł18-Dec-12 4:30 
GeneralRe: why so complicate? Pin
Keith Barrow18-Dec-12 4:49
professionalKeith Barrow18-Dec-12 4:49 
GeneralMy vote of 5 Pin
Alberto Richart28-Jun-12 2:58
Alberto Richart28-Jun-12 2:58 
GeneralMy vote of 5 Pin
h3y26-Jun-12 4:48
h3y26-Jun-12 4:48 

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.