.NET 拆除 Windows 服务

发布于 2024-08-15 18:40:27 字数 1555 浏览 7 评论 0原文

我正在构建一个简单的 Windows 服务,但遇到了一个小问题。

该服务运行良好,OnStart 方法创建一个工作进程来侦听传入的 UDP 连接。

我遇到的问题是,当我单击服务上的“停止”或“重新启动”时,该服务在任务管理器中保持运行状态。不确定我做错了什么。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-08-22 18:40:27

我这个周末发布了基本上相同的问题,您可能想看看我在那里得到的答案:

如何让我的 TCP 侦听器服务正确终止?

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文