gpt4 book ai didi

.net - 后台工作线程导致 UI 挂起

转载 作者:行者123 更新时间:2023-12-04 06:47:04 25 4
gpt4 key购买 nike

我有一个后台工作人员正在检查远程服务器上四个服务的状态。
这是在计时器(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

最佳答案

这个while循环是完全没有必要的:

While _ServiceBGWorker.IsBusy
Application.DoEvents()
End While

我不明白它是如何在这里导致您的直接问题的,但您可以尝试不使用。

在相关说明中,最好添加一个 Completed 处理程序并检查 e.Error 状态。也许有些东西正在抛出异常。使用您当前的代码,您将永远不会知道。

关于.net - 后台工作线程导致 UI 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3596910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com