.NET拆解Windows服务

时间:2009-12-21 16:35:01

标签: .net windows-services worker-process

我正在构建一个简单的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

1 个答案:

答案 0 :(得分:1)

本周末我发布了基本相同的问题,你可能想看看我到那里的答案:

How can I get my TCP listener service to terminate correctly?