开发者

Backgroundworker thread causing UI to hang

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开发者_StackOverflow社区
        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


It wouldn't hurt to review the MSDN Background Worker Class documentation. A note that reads

You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

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.


This while loop is completely unnecessary:

While _ServiceBGWorker.IsBusy
    Application.DoEvents()
End While

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜