Wednesday, February 11, 2009

Debugging and Windows Service in VB.Net

Perhaps this is helpful, perhaps not.  I've spent the last 45 min looking over various blog posts and .Net sites trying to find an easy way to debug an NT service.  I kept coming across, install the serivce, start it, attached  a debugger to a process...etc.  I know there is an easy way to do it because I've used it several years ago in .Net 1.1.  I gave up searching and went looking through old source code and found it. 'Tis below:


'****Modified by JWear on 4/12/2006****

Private Sub New()
If Debugger.IsAttached = True Then
' debug code: allows the process to run as a non-service
' will kick off the service start point, but never kill it.
' shut down the debugger to exit
'Open this page for help ' 'http://www.codeproject.com/dotnet/DebugWinServices.asp
Dim service As New YourService
Dim tempargs() As String
service.OnStart(tempargs)
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
Else
'Original Code Below
ServicesToRun = New System.ServiceProcess.ServiceBase() {New YourService}


System.ServiceProcess.ServiceBase.Run(ServicesToRun)
'Original Code Above
End If
'**************

End Sub

I left the page where I got it in the comments, a hat tip to the original site. The page is no longer available.

No comments:

Post a Comment

Post a Comment