in my program i use a Datatable which is filled at loading of form. And i use backgroundworker to do some network operations. Inside doWork() of backgroundworker i need data from datatable and also object of a class that i created in loading of form. Data in datatable and class object is updated inside Dowork() and also this values from datatable and class object is updated in a listivew in ProgressChanged of backgroundworker. Both datatable and class object is declared global to Form1. But i read this is not thread safe.So how to use in correct to use it in thread safe way?
Asked
Active
Viewed 1,723 times
0
-
Take a look at locking and/or parameterized ThreadStart. – Tim Oct 28 '12 at 06:55
1 Answers
0
You can pass one argument from RunWorkerAsync to DoWork, and this can be an array to get it all over:
dim bgw as new BackgroundWorker
dim dt as DataTable
dim anything as ...
...
bgw.RunWorkerAsync(New Object() {dt, anything, ...})
...
inside doWork:
Private Sub BGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
Dim dt as DataTable
dt = TryCast(e.Argument(0), DataTable)
' and so on
As soon as you start to modify anything UI-related on a form, you can start reading about BeginInvoke and InvokeRequired. There's probably a lot out there, but maybe this question I had may be a good sample for this:
Multithreading for a progressbar and code locations (vb.net)?