.NET 拆除 Windows 服务
我正在构建一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这个周末发布了基本上相同的问题,您可能想看看我在那里得到的答案:
如何让我的 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?