0

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?

Vpp Man
  • 2,384
  • 8
  • 43
  • 74

1 Answers1

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)?

Community
  • 1
  • 1
KekuSemau
  • 6,830
  • 4
  • 24
  • 34