Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 1.25 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to pass an array as a parameter for doWork in C#
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4.     BackgroundWorker worker = new BackgroundWorker;
  5.     worker.WorkerReportsProgress = true;
  6.     worker.ProgressChanged += ProgressChanged;
  7.     worker.DoWork += ReadStream;
  8.     //need to pass the comoBox Texts from here!!!
  9.     string start = comboBox1.Text;
  10.     string end = comboBox2.Text;
  11.     worker.RunWorkerAsync();
  12. }
  13.  
  14. private void ProgressChanged(object sender, ProgressChangedEventArgs e)
  15. {
  16.     UpdateProgressBar(e.ProgressPercentage);
  17.     comboBox1.Text = e.UserState.ToString();
  18. }
  19.  
  20. private void ReadStream(object sender, DoWorkEventArgs doWorkEventArgs)
  21. {
  22.  
  23.     BackgroundWorker worker = sender as BackgroundWorker;
  24.     string line;
  25.     //and use the values here !!!!
  26.     using (StreamReader sr = new StreamReader("file", System.Text.Encoding.ASCII))
  27.     {
  28.         while (!sr.EndOfStream)
  29.         {
  30.             line = sr.ReadLine();
  31.             worker.ReportProgress(line.Length);
  32.         }
  33.  
  34.     }
  35. }
  36.        
  37. string[] texts = new string[] {start, end};
  38. worker.RunWorkerAsync(texts);
  39.        
  40. string[] extracted = (string[])doWorkEventArgs.Argument;
  41. string start = extracted[0];
  42. string end = extracted[1];
  43.        
  44. worker.RunWorkerAsync(array);
  45.        
  46. doWorkEventArgs.Argument