Click here to Skip to main content
13,351,682 members (44,387 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 12 Sep 2007

Debugging OnStart Method in .NET Service when Debbuger.Launch Doesn't Work

Rate this:
Please Sign up or sign in to vote.
Technique to attach a debugger to a starting service project


A lot of workarounds exist to debug the OnStart method of a service. A straight way with Debugger.Launch() is described in How to Debug a Windows Service. But what if this wouldn't work, e.g. on Windows Vista x64? This sample shows a simple method to attach the debugger to the service process and debug the OnStart method.

How It Works

The service process should wait till you have attached the debugger. The service process should not timeout. Just look at the code, it's quite easy.

Using the Code

All you have to do, is to copy this code snippet in your Onstart method of your service class (derived from ServiceBase).

Using Directives

using System.Diagnostics;  // a quite useful namespace for debugging issues
using System.Threading;

OnStart Method

protected override void OnStart(string[] args)
    while (!Debugger.IsAttached)      // Waiting until debugger is attached
        RequestAdditionalTime(1000);  // Prevents the service from timeout
        Thread.Sleep(1000);           // Gives you time to attach the debugger   
    RequestAdditionalTime(20000);     // for Debugging the OnStart method,
                                      // increase as needed to prevent timeouts
    // here is your startup code with breakpoints

Start the service as usual. The service will wait until you attach the debugger to the service process. You can set breakpoints in your start code and debug it.


  • 12th September, 2007: Initial version tested on Vista x64 / VS2005 with a x64 compiled C# project


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework for Microsoft .NET and Mono. Please take a look at it:
Crawler-Lib Homepage
Crawler-Lib Blog
Crawler-Lib YouTube Channel

You may also be interested in...


Comments and Discussions

GeneralSimpler solution Pin
Julien Couvreur13-Sep-07 12:32
memberJulien Couvreur13-Sep-07 12:32 
AnswerRe: Simpler solution - but not nice Pin
MSE-iT13-Sep-07 16:37
memberMSE-iT13-Sep-07 16:37 
This is not very nice, because you have to break the app and modify a variable to leave the loop. I just attach my debugger and the service will automatically continue. Exeptions and real breakpoints will stop the service. If you are not quick enough, your service gets an timeout. Your loop is also time consuming, this may affect the GUI. I can't see any benefit in your solution. Just copy this view lines of code and you get a better result. Greetings Tom

modified 4-Dec-14 5:56am.

GeneralDebugger.Launch(); Pin
Mladen Jankovic12-Sep-07 12:53
memberMladen Jankovic12-Sep-07 12:53 
GeneralRe: Debugger.Launch(); [modified] Pin
MSE-iT12-Sep-07 23:22
memberMSE-iT12-Sep-07 23:22 
GeneralRe: Debugger.Launch(); Pin
Eduard Gomolyako13-Sep-07 2:30
memberEduard Gomolyako13-Sep-07 2:30 
AnswerRe: Debugger.Launch(); Pin
MSE-iT13-Sep-07 3:17
memberMSE-iT13-Sep-07 3:17 
AnswerRe: Debugger.Launch(); Pin
Mladen Jankovic13-Sep-07 6:57
memberMladen Jankovic13-Sep-07 6:57 
AnswerRe: Debugger.Launch(); Pin
MSE-iT13-Sep-07 7:11
memberMSE-iT13-Sep-07 7:11 
GeneralSolution for Debugger.Launch Pin
KyronSr8-Jul-08 9:13
memberKyronSr8-Jul-08 9:13 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 12 Sep 2007
Article Copyright 2007 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid