By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Correct method for constantly updating DGV via backgroundWorker

100+
P: 116
Hi,

I am using a backgroundWorker 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 backgroundWorker. 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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub dataCheckingBackground_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles dataCheckingBackground.DoWork
  2. Dim checkcount As Integer = 0
  3. Do
  4.    checkcount += 1
  5.    dataCheckingBackground.ReportProgress(checkcount)
  6.    Threading.Thread.Sleep(30000)
  7.    If dataCheckingBackground.CancellationPending = True Then Exit Do
  8. Loop
  9. End Sub
  10.  
  11. Private Sub dataCheckingBackground_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles dataCheckingBackground.RunWorkerCompleted
  12. dataCheckingBackground.CancelAsync()
  13. End Sub
  14.  
  15. Private Sub dataCheckingBackground_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles dataCheckingBackground.ProgressChanged
  16. ' this gets the third party data and processes into DGV etc
  17. End Sub
  18.  
Many thanks!
Dec 19 '14 #1

✓ answered by robertybob

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.

Share this Question
Share on Google+
2 Replies


100+
P: 116
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.
Dec 20 '14 #2

100+
P: 116
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.
Dec 28 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.