views:

41

answers:

1

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
+1  A: 

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.

Henk Holterman
I wouldn't be surprised if this loops is causing the problem, tho'.
Will A
Ahh, removing the loop solved the problem - I thought this was required to update the pictureboxes.
madlan
@madian: But do fix your exception handling too. Make it a habit.
Henk Holterman