后台工作线程导致 UI 挂起
我有一个后台工作人员正在检查远程服务器上四个服务的状态。 这是在计时器(5 秒)上设置的,如下所示。由于某种原因,它挂起 UI 线程,导致应用程序每个时钟周期“锁定”一秒,我不明白为什么?!
Private Sub ServiceTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServiceTimer.Tick
_ServiceBGWorker = New System.ComponentModel.BackgroundWorker()
_ServiceBGWorker.WorkerSupportsCancellation = False
_ServiceBGWorker.WorkerReportsProgress = False
AddHandler _ServiceBGWorker.DoWork, New DoWorkEventHandler(AddressOf Me.CheckService)
_ServiceBGWorker.RunWorkerAsync()
While _ServiceBGWorker.IsBusy
Application.DoEvents()
End While
End Sub
Private Sub CheckService(ByVal sender As Object, ByVal e As DoWorkEventArgs)
If CheckService("Service 3.5") = ServiceControllerStatus.Stopped Then
PBServiceStatus35.Image = ImgStopIcon
ElseIf CheckService("Service 3.5") = ServiceControllerStatus.Running Then
PBServiceStatus35.Image = ImgGoIcon
Else
PBServiceStatus35.Image = ImgHelpIcon
End If
If CheckService("Service 3.6") = ServiceControllerStatus.Stopped Then
PBServiceStatus36.Image = ImgStopIcon
ElseIf CheckService("Service 3.6") = ServiceControllerStatus.Running Then
PBServiceStatus36.Image = ImgGoIcon
Else
PBServiceStatus36.Image = ImgHelpIcon
End If
If CheckService("Service 3.7") = ServiceControllerStatus.Stopped Then
PBServiceStatus37.Image = ImgStopIcon
ElseIf CheckService("Service 3.7") = ServiceControllerStatus.Running Then
PBServiceStatus37.Image = ImgGoIcon
Else
PBServiceStatus37.Image = ImgHelpIcon
End If
If CheckService("Service 4.0") = ServiceControllerStatus.Stopped Then
PBServiceStatus40.Image = ImgStopIcon
ElseIf CheckService("Service 4.0") = ServiceControllerStatus.Running Then
PBServiceStatus40.Image = ImgGoIcon
Else
PBServiceStatus40.Image = ImgHelpIcon
End If
End Sub
Private Function CheckService(ByVal ServiceName As String)
Dim myController = New ServiceController(ServiceName)
myController.MachineName = SQLServerName
myController.Refresh()
Return myController.Status
End Function
I have a background worker that's checking the status of four services on a remote server.
This is setup on a timer (5 Seconds) as below. For some reason it's hanging the UI thread causing the application to 'lock' for a second each tick, I cannot work out why?!
Private Sub ServiceTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServiceTimer.Tick
_ServiceBGWorker = New System.ComponentModel.BackgroundWorker()
_ServiceBGWorker.WorkerSupportsCancellation = False
_ServiceBGWorker.WorkerReportsProgress = False
AddHandler _ServiceBGWorker.DoWork, New DoWorkEventHandler(AddressOf Me.CheckService)
_ServiceBGWorker.RunWorkerAsync()
While _ServiceBGWorker.IsBusy
Application.DoEvents()
End While
End Sub
Private Sub CheckService(ByVal sender As Object, ByVal e As DoWorkEventArgs)
If CheckService("Service 3.5") = ServiceControllerStatus.Stopped Then
PBServiceStatus35.Image = ImgStopIcon
ElseIf CheckService("Service 3.5") = ServiceControllerStatus.Running Then
PBServiceStatus35.Image = ImgGoIcon
Else
PBServiceStatus35.Image = ImgHelpIcon
End If
If CheckService("Service 3.6") = ServiceControllerStatus.Stopped Then
PBServiceStatus36.Image = ImgStopIcon
ElseIf CheckService("Service 3.6") = ServiceControllerStatus.Running Then
PBServiceStatus36.Image = ImgGoIcon
Else
PBServiceStatus36.Image = ImgHelpIcon
End If
If CheckService("Service 3.7") = ServiceControllerStatus.Stopped Then
PBServiceStatus37.Image = ImgStopIcon
ElseIf CheckService("Service 3.7") = ServiceControllerStatus.Running Then
PBServiceStatus37.Image = ImgGoIcon
Else
PBServiceStatus37.Image = ImgHelpIcon
End If
If CheckService("Service 4.0") = ServiceControllerStatus.Stopped Then
PBServiceStatus40.Image = ImgStopIcon
ElseIf CheckService("Service 4.0") = ServiceControllerStatus.Running Then
PBServiceStatus40.Image = ImgGoIcon
Else
PBServiceStatus40.Image = ImgHelpIcon
End If
End Sub
Private Function CheckService(ByVal ServiceName As String)
Dim myController = New ServiceController(ServiceName)
myController.MachineName = SQLServerName
myController.Refresh()
Return myController.Status
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 MSDN 后台工作器类文档。一条注释写着
因此,不要在 DoWork 处理程序中操作图片框,而是使用上述处理程序。我编写了两个简单的表单,并从 DoWork 处理程序加载了一个,当从 DoWork 加载该表单时,该表单和 UI 无限期挂起,但从其他表单正确响应。
It wouldn't hurt to review the MSDN Background Worker Class documentation. A note that reads
So instead of manipulating your picturebox in your DoWork handler, use the aforementioned handlers. I wrote two simple forms and loaded one from a DoWork handler and the Form and UI hang indefinitely when the Form is loaded from DoWork, but responds properly from the others.
这个 while 循环是完全不必要的:
虽然我不明白它如何会导致你的直接问题,但你可以尝试不使用。
与此相关的是,最好添加一个 Completed 处理程序并检查 e.Error 状态。也许有什么东西抛出异常。使用您当前的代码,您永远不会知道。
This while loop is completely unnecessary:
I don't see how it could cause your direct problem here though, but you might try without.
On a related note, it would be a good idea to add a Completed handler and check the e.Error status. Maybe something is throwing exceptions. With your current code, you will never know.