|
-
Feb 12th, 2019, 07:58 AM
#1
Fanatic Member
Re: Correct way to use the BackgroundWorker
OMG, that easy and still so far for me. Thank you. Can you please tell me why that first parameter should be zero and not 100?
-
Feb 12th, 2019, 08:57 AM
#2
Re: Correct way to use the BackgroundWorker
 Originally Posted by Grand
Can you please tell me why that first parameter should be zero and not 100?
The first parameter should be whatever you want it to be. If you are actually going to use the value to provide feedback to the user then it should accurately represent the current progress. If you're not going to use it for anything in the ProgressChanged event handler then what's the point of using anything other than zero?
I'm not convinced that you're doing the right thing here though. You are passing a DataTable and you seem to be indicating that the progress is at 100%, i.e. the background work is complete. If you are trying to pass data back to the UI thread when the work is complete then you should not be calling ReportProgress and handling ProgressChanged. Instead, you should be setting the e.Result property and letting the DoWork event handler complete. You then handle the RunWorkerCompleted event and get the data back from the e.Result property there. If this DataTable is the result of the backgroundwork then that is how it should be handled. ReportProgress/ProgressChanged is just to keep the user updated along the way. That might involve just the numerical progress or it might be a batch of data that you have processed so far but if you are only transferring data in a single block at the end, don't do it using ReportProgress/ProgressChanged.
-
Feb 12th, 2019, 12:57 PM
#3
Fanatic Member
Re: Correct way to use the BackgroundWorker
 Originally Posted by jmcilhinney
If you are trying to pass data back to the UI thread when the work is complete then you should not be calling ReportProgress and handling ProgressChanged.
Instead, you should be setting the e.Result property and letting the DoWork event handler complete. You then handle the RunWorkerCompleted event and get the data back from the e.Result property there. If this DataTable is the result of the backgroundwork then that is how it should be handled.
Spot on. That is exactly what I wanted to do. Would you also please tell me how exactly I set e.result property? And how I bring back the table so I can bind it with the datagridview?
Thanks.
I tried this but the table is not returend here:
Code:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Dim MyDataTable As DataTable = DirectCast(e.Result, DataTable)
DataGridView2.DataSource = MyDataTable
SpinnerForm.Close() 'Close the form with spinner
End Sub
-
Feb 12th, 2019, 05:36 PM
#4
Re: Correct way to use the BackgroundWorker
 Originally Posted by Grand
Spot on. That is exactly what I wanted to do. Would you also please tell me how exactly I set e.result property? And how I bring back the table so I can bind it with the datagridview?
Thanks.
I tried this but the table is not returend here:
Code:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Dim MyDataTable As DataTable = DirectCast(e.Result, DataTable)
DataGridView2.DataSource = MyDataTable
SpinnerForm.Close() 'Close the form with spinner
End Sub
That part is right but you didn't put the DataTable there in the first place to get it back again. I told you exactly what to do:
Instead, you should be setting the e.Result property and letting the DoWork event handler complete.
How can you be asking how to set a property? You're already doing it in code you already have. The code you just posted sets the DataSource property of a DataGridView. Why should setting any other property be any different?
Last edited by jmcilhinney; Feb 12th, 2019 at 06:31 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|