Click here to Skip to main content
12,622,517 members (32,859 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.8K views
6 bookmarked
Posted

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

, 28 Apr 2010 CPOL
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)

Share

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWindows form Pin
FELIX 102206912-Apr-15 19:23
memberFELIX 102206912-Apr-15 19:23 
AnswerRe: Windows form Pin
Keith Barrow3-Apr-15 13:42
mentorKeith Barrow3-Apr-15 13:42 
QuestionRe: Windows form Pin
FELIX 102206913-Apr-15 19:13
memberFELIX 102206913-Apr-15 19:13 
Suggestionwhy so complicate? Pin
Adam Wyżgoł18-Dec-12 5:30
memberAdam Wyżgoł18-Dec-12 5:30 
GeneralRe: why so complicate? Pin
Keith Barrow18-Dec-12 5:49
mentorKeith Barrow18-Dec-12 5:49 
GeneralMy vote of 5 Pin
Alberto Richart28-Jun-12 3:58
memberAlberto Richart28-Jun-12 3:58 
GeneralMy vote of 5 Pin
h3y26-Jun-12 5:48
memberh3y26-Jun-12 5: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 28 Apr 2010
Article Copyright 2010 by Keith Barrow
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid