Reputation: 11
I have made an application which runs three backgroundworkers simultaneously. It basically updates three datagridviews, which it is doing spot on. My problem is if I press maximizebox button or anywhere in any of the datagridview the program hangs for quite a long time. I am able to use the horizontal scroll but not vertical scrolls. I have tried Backgroundworker_runworkercompleted and it fires as required after threads have updated their respective datagridviews. Is it a normal behaviour or am i doing something wrong any suggestions would be helpful. P.S: I have run the whole program using step method and their is no infinite loop in the code. Thanks in advance Jhon
Upvotes: 1
Views: 3171
Reputation: 353
It's too late to answer to @Jhon but it seems like it might help other people. In my case, I was using a BindingSource and the UI only froze if there where so many records that scrollbars had to appear.
When you use a background worker, task or thread to do the work, you have to update your UI controls on the main UI thread. To do that you can Invoke a method in the corresponding thread.
A simple example:
myDataGridView.Invoke((MethodInvoker)delegate { myBindingSource.DataSource = myData; });
But i prefer this solution: InvokeIfRequired
Upvotes: 0
Reputation: 405
I had the same problem. When my dataGridViews were being updated from the UI thread, they worked fine. When I tried to add a series of rows to them from the backgroundWorker, they became unresponsive.
To correct this, I added the rows to a dataTable instead. When I kick off the backgroundWorker, I set the .datasource property of the dataGridView to nothing. When the worker completes, I set it back to the dataTable again. This both forces the dataGridView to update itself, and severs the synchronous connection between what it happening in the worker thread, and what is being displayed in the UI, which seems to alleviate the unresponsiveness when the worker completes.
Sample usage:
Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If bgWorker1.IsBusy <> True Then
dataGridView1.DataSource = Nothing
bgWorker1.RunWorkerAsync()
End If
End Sub
Private Sub bgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bgWorker1.DoWork
dataTable1.Rows.Add("data")
End Sub
Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgWorker1.RunWorkerCompleted
dataGridView1.DataSource = dataTable1
dataGridView1.Refresh()
End Sub
Upvotes: 0
Reputation: 2163
You have to make sure that you are updating the datagridviews from the UI thread.
Upvotes: 0
Reputation: 2283
Place this line of code just before when you are calling Backgroundworker1.RunWorkerAsync()
to temporarily disable your datagridview scroll bars.
DataGridView1.ScrollBars = ScrollBars.None
Now Re-enable scroll bars of your datagridview by adding this line of code in BackgroundWorker1_RunWorkerCompleted
event.
DataGridView1.ScrollBars = ScrollBars.Both
This will never make your application unresponding.
Upvotes: 3
Reputation: 1
Okay I have found the solution to my problem, while working out sequential elimination of the perceived trouble spots, I called my datagridview outside of the backgroundworker.dowork event and voila that solved the problem. Moral of the story "NEVER UPDATE A DATA GRID VIEW FROM WITHIN THE BACKGROUNDWORKER THREAD" specially when you don't know what are you doing wrong :). I hope it will help someone in future.
Upvotes: 0
Reputation: 134247
It sounds like you are still blocking the UI thread somehow. It may be helpful for you to post some code snippets. Also, what is the CPU utilization of your process? If the cpu usage is high, you may be starving the UI thread somehow.
Upvotes: 0