Correct method for constantly updating DGV via backgroundWorker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Correct method for constantly updating DGV via backgroundWorker

    Hi,

    I am using a backgroundWorke r to access an API for share prices. The response is then parsed and values updated on a DGV on the main UI. The call is made every 30 seconds for new information.

    The method below does all this ok however the main UI will freeze during the data retrieval and DGV update despite the fact that the processing is done on the backgroundWorke r. I am making sure that only values that have changed are actually re-entered into the DGV.

    Is there a proper method for doing this efficiently such that the UI never freezes? Do I need to 'invoke' anything?

    Current method as follows.

    Code:
    Private Sub dataCheckingBackground_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles dataCheckingBackground.DoWork
    Dim checkcount As Integer = 0
    Do
       checkcount += 1
       dataCheckingBackground.ReportProgress(checkcount)
       Threading.Thread.Sleep(30000)
       If dataCheckingBackground.CancellationPending = True Then Exit Do
    Loop
    End Sub
    
    Private Sub dataCheckingBackground_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles dataCheckingBackground.RunWorkerCompleted
    dataCheckingBackground.CancelAsync()
    End Sub
    
    Private Sub dataCheckingBackground_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles dataCheckingBackground.ProgressChanged
    ' this gets the third party data and processes into DGV etc
    End Sub
    Many thanks!
  • robertybob
    New Member
    • Feb 2013
    • 116

    #2
    I'm currently waiting to investigate this but I'm thinking that my problem is that my reportProgress (ie the UI) is doing all the data retrieval and updating and my doWork (ie the BW) is simply dealing with the timing.

    I think my doWork should be doing everything possible and only passing the results to reportProgress for it to update the UI.

    This should speed things up significantly to the point of being unnoticeable. Will report back when have a chance to check.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      It seems this is the main cause of my problems so for those in a similar situation, remember that the things needed to be done in the background must be in the DoWork and the things required for the UI in the reportProgress.

      Comment

      Working...