How to debug a Windows service using breakpoints?

I have a windows service with a timer. It is very difficult to debug. Because I start the service and set breakpoints in different parts of the code. When I attach this process, I expect the service to start from the very beginning, and not in some random place in the middle code where I have breakpoints. It is difficult to debug like a regular application, where you know the starting point. It seems that the processes in the background are not yet complete. Therefore, every time I start debugging, instead of starting from the very first breakpoint, it starts with some random breakpoint in the middle of the application.

I want to know how the Windows service works in terms of processes, threads, etc ... and how can I start debugging from the very beginning?

+5
source share
5 answers

I assume you say .Net, of course. I have always used the following code to debug my services. I put it where I want to run the debugger. Start the service and it automatically starts Visual Studio. Works well for me.

System.Diagnostics.Debugger.Launch();

System.Diagnostics.Debugger.Debug();

+9
source

Just hit F5. You can run Windows services like regular applications.

Since we have no other command line arguments, we simply use the presence of any command line argument as a signal to run as a regular Windows application. You can also require a specific command line argument (i.e. / debug).

If sArgs IsNot Nothing AndAlso sArgs.Length > 0 Then
    ' If there are command-line args then run in non-service mode
    Dim svc As ServiceMain = New ServiceMain(True)
    svc.OnStart(Nothing)
    System.Windows.Forms.Application.Run()
    svc.OnStop()
Else
    ' If no command-line args then run normally
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New ServiceMain(False)}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End If
+2

" " , , , .
WinDBG Visual Studio.

+1

...

- , ... ...

0

, , , . , .

0
source

All Articles