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