Home All Groups Group Topic Archive Search About

Debug Windows Services

Author
10 Aug 2006 7:14 PM
Elioth
Hi,

How I Debug a Windows Services in VB 2005?

Thanks for all help.

Elioth

Author
11 Aug 2006 2:08 PM
Brian Gideon
Elioth,

You have a couple of options.

1) Start the service in the Service Control Manager and attach the VS
debugger to a running process via Debug | Processes.  I don't like this
option because the application may have already performed a lot of work
before you can attach and set a breakpoint.

2) Setup the application to run either as a service or a console
application.  It's much easier to debug a console application.  The
following code demonstrates what I'm talking about.  There are several
variations on this theme including the use of conditional compilation.

Shared Sub Main()

  If Environment.UserInteractive Then

    ' Run as a console application.
    Dim service As Service1 = New Service1
    service.OnStart(Nothing)
    Console.WriteLine("Press ENTER to quit...")
    Console.ReadLine()
    service.OnStop()

  Else

    ' Assume the Service Control Manager invoked the application.
    Dim ServicesToRun() As ServiceBase
    ServicesToRun = New ServiceBase () {New Service1}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)

  End If

End Sub

Brian


Elioth wrote:
Show quoteHide quote
> Hi,
>
> How I Debug a Windows Services in VB 2005?
>
> Thanks for all help.
>
> Elioth
Author
14 Aug 2006 4:27 PM
Claes Bergefall
A third option is to P/Invoke the DebugBreak method.

    /claes

Show quoteHide quote
"Brian Gideon" <briangid***@yahoo.com> wrote in message
news:1155305289.596195.35830@i42g2000cwa.googlegroups.com...
> Elioth,
>
> You have a couple of options.
>
> 1) Start the service in the Service Control Manager and attach the VS
> debugger to a running process via Debug | Processes.  I don't like this
> option because the application may have already performed a lot of work
> before you can attach and set a breakpoint.
>
> 2) Setup the application to run either as a service or a console
> application.  It's much easier to debug a console application.  The
> following code demonstrates what I'm talking about.  There are several
> variations on this theme including the use of conditional compilation.
>
> Shared Sub Main()
>
>  If Environment.UserInteractive Then
>
>    ' Run as a console application.
>    Dim service As Service1 = New Service1
>    service.OnStart(Nothing)
>    Console.WriteLine("Press ENTER to quit...")
>    Console.ReadLine()
>    service.OnStop()
>
>  Else
>
>    ' Assume the Service Control Manager invoked the application.
>    Dim ServicesToRun() As ServiceBase
>    ServicesToRun = New ServiceBase () {New Service1}
>    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
>
>  End If
>
> End Sub
>
> Brian
>
>
> Elioth wrote:
>> Hi,
>>
>> How I Debug a Windows Services in VB 2005?
>>
>> Thanks for all help.
>>
>> Elioth
>