2009-12-21 79 views
1

我正在構建一個簡單的Windows服務,我遇到了一個小問題。.NET撕下Windows服務

服務運行良好,OnStart方法創建一個工作進程,用於偵聽傳入的UDP連接。

我遇到的問題是,當我單擊服務上的STOP或RESTART時,該服務將保持在任務管理器中運行。不知道我做錯了什麼。

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 

回答