views:

42

answers:

1

I'm in the middle of building a simple windows service and I'm running into a small issue.

The service runs just fine, the OnStart method creates a worker process that listens for incoming UDP connections.

The problem I'm having is that when I either click STOP on the service, or RESTART, the service stays running in the task manager. Not sure what I'm doing wrong.

Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Text

Public Class Service1
Private ListenSocket As New MyNameSpace.Logging
Private wt As System.Threading.Thread

Protected Overrides Sub OnStart(ByVal args() As String)
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionEventRaised

    'Load Initial IP Details'
    Try
        Dim logger As New MyNameSpace.Logging
        logger.LoadIPDetails()
    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try

    'Start the listener in a new worker
    Try
        Dim ts As System.Threading.ThreadStart
        ts = AddressOf ListenSocket.ListenForSyslogs
        wt = New System.Threading.Thread(ts)
        wt.Start()
    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try
End Sub

Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
    Try
        wt.Abort()
        wt = Nothing

    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try
End Sub

Protected Overloads Sub UnhandledExceptionEventRaised(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    If e.IsTerminating Then
        Dim o As Object = e.ExceptionObject
        MyNameSpace.ErrorLogging.Log(o) ' use EventLog instead
    End If
End Sub


End Class
+1  A: 

I posted basically the same question this weekend, you may want to look at the answer I got there:

http://stackoverflow.com/questions/1934746/how-can-i-get-my-tcp-listener-service-to-terminate-correctly

davidcl