Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#
Tip/Trick

How to Debug Windows Services and Plugins

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jun 2010CPOL 12.5K   9   2
How to Programatically (and configurably) Launch a Debugger
If you’ve ever had to create an component hosted inside a third party application (such as a plugin or Windows service), you’ll know that it can be pretty tricky to get the debugger attached to the process quickly and reliably. Here’s a snippet that I’ve taken from project to project:

C#
#if DEBUG
            if (Properties.Settings.Default.LaunchDebugger
                && !System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Launch();
#endif


This statement checks to see if launching the debugger is allowed (using the standard settings classes) and launches the debugger if one isn’t already attached.

For release builds, the entire line is removed from code to avoid ‘accidental’ debugging.

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) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

Comments and Discussions

 
GeneralReason for my vote of 5 That's considerably better than my #... Pin
TheyCallMeMrJames28-Jun-10 10:32
TheyCallMeMrJames28-Jun-10 10:32 
GeneralThat's considerably better than my #if DEBUG...Sleep(15000) ... Pin
TheyCallMeMrJames28-Jun-10 10:31
TheyCallMeMrJames28-Jun-10 10:31 

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.