I have limited experience in VB.net and have become stuck. I have tried to search for answers on other threads without success.
I have an application that displays real time data.
Queries are made to a MySQL database on another PC over the internet every 10 seconds to gather the most recent data.
I have done the query within a BackgroundWorker so that the GUI is still responsive while the query is answered.
What I've done works great for several hours or maybe for a couple of days but then the DoWork task appears to hang.
The code I'm using is to run the background worker is:
vb Code:
If Not(backgroundWorker1.IsBusy) Then
Me.backgroundWorker1.RunWorkerAsync()
End If
The backgroundWorker runs just once and there's very little code in the DoWork routine:
vb Code:
Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork
rs = New ADODB.Recordset
If Not (oConn.State = 1) Then
oConn.Open(strConn)
End If
rs.Open(strSQL, oConn, ADODB.CursorTypeEnum.adOpenStatic)
MySQLData = rs.GetRows
End Sub
Once the backgroundWorker completes the RunWorkerCompleted task runs:
vb Code:
Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
StartUp.StartUpMessages("Data " & e.Error.Message)
End If
If IsArray(MySQLData) Then
'use the data
Else 'error in collecting alarms
StartUp.StartUpMessages("No Data returned from database - Data")
End If
MySQLData = 0
End Sub
Most of the time everything is working perfectly - the data is collected correctly. If I break the connection to the MySQL database by disconnecting my PC from the internet then the DoWork task correctly generates an error and the RunWorkerCompleted task fires and gives the correct error.
But occasionally the DoWork task is not completing so that the backgroundWorker is always busy and so the DoWork task can't be run again. It appears that the DoWork task has hung but is not generating an error.
Is there anyway I can set up a timeout on the maximum allowed time that the DoWork task can be active for? And if so how would I then go on to stop the backgroundWorker?
I understand that CancelAsync isn't going to help as my DoWork task is not repeating as it runs once only, then stops and 10 secs later I run it again.
I would appreciate any help that someone with greater knowledge than me can give.
Thank you for reading.