Results 1 to 2 of 2

Thread: VB.Net BackgroundWorker hangs up

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    6

    VB.Net BackgroundWorker hangs up

    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:
    1. If Not(backgroundWorker1.IsBusy) Then
    2.             Me.backgroundWorker1.RunWorkerAsync()
    3.         End If

    The backgroundWorker runs just once and there's very little code in the DoWork routine:

    vb Code:
    1. Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork
    2.         rs = New ADODB.Recordset
    3.         If Not (oConn.State = 1) Then
    4.             oConn.Open(strConn)
    5.         End If
    6.         rs.Open(strSQL, oConn, ADODB.CursorTypeEnum.adOpenStatic)
    7.         MySQLData = rs.GetRows
    8.     End Sub

    Once the backgroundWorker completes the RunWorkerCompleted task runs:

    vb Code:
    1. Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
    2.         If e.Error IsNot Nothing Then
    3.             StartUp.StartUpMessages("Data " & e.Error.Message)
    4.         End If
    5.  
    6.         If IsArray(MySQLData) Then
    7. 'use the data
    8.         Else 'error in collecting alarms
    9.             StartUp.StartUpMessages("No Data returned from database - Data")
    10.         End If
    11.  
    12.         MySQLData = 0
    13.  
    14.     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.
    Last edited by gep13; Mar 22nd, 2011 at 02:22 AM. Reason: Added Highlight tags

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: VB.Net BackgroundWorker hangs up

    Hello workchill,

    Welcome to the forums!

    When you are posting code into the forum, can you please remember to surround it in [code][/code] or [HIGHLIGHT=vb][/highlight] tags? It makes it a lot easier to read. I have done this in your above post.

    Thanks

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width