How to pass an array as parameter for doWork in C #

I need to use form controls such as comboBox1.text and comboBox2.Text inside the readstream function, and deliver an error message (translated from German):

Access control comboBox1 / comboBox2 comes from a different stream, and not from the stream in which it is created !!!

I need to pass these controls to the readstream function, but I don't know exactly how.

Code

private void button1_Click(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker; worker.WorkerReportsProgress = true; worker.ProgressChanged += ProgressChanged; worker.DoWork += ReadStream; //Need to pass the comoBox Texts from here!!! string start = comboBox1.Text; string end = comboBox2.Text; worker.RunWorkerAsync(); } private void ProgressChanged(object sender, ProgressChangedEventArgs e) { UpdateProgressBar(e.ProgressPercentage); comboBox1.Text = e.UserState.ToString(); } private void ReadStream(object sender, DoWorkEventArgs doWorkEventArgs) { BackgroundWorker worker = sender as BackgroundWorker; string line; //And use the values here !!!! using (StreamReader sr = new StreamReader("file", System.Text.Encoding.ASCII)) { while (!sr.EndOfStream) { line = sr.ReadLine(); worker.ReportProgress(line.Length); } } } 
+4
source share
2 answers

Before calling worker.RunWorkerAsync(); do the following:

 string[] texts = new string[] {start, end}; worker.RunWorkerAsync(texts); 

Then in ReadStream(...)

 string[] extracted = (string[])doWorkEventArgs.Argument; string start = extracted[0]; string end = extracted[1]; 
+9
source

Try this code pass the array as a parameter:

 worker.RunWorkerAsync(array); 

Use this code to get this array:

 doWorkEventArgs.Argument 
+3
source

Source: https://habr.com/ru/post/1387748/


All Articles