[英]How to pass an array as a parameter for doWork in C#
I need to use form controls like comboBox1.text and comboBox2.Text inside the readstream function an this deliver an error message (translated from German): 我需要在readstream函数中使用comboBox1.text和comboBox2.Text等表单控件,这会传递一条错误消息(翻译自德语):
The access of control element comboBox1/comboBox2 is from another thread rather than the thread in which it is created in !!!
控制元素comboBox1 / comboBox2的访问来自另一个线程,而不是它在其中创建的线程!
What I need is to pass these controls to the readstream function, but I don't know how exactly. 我需要的是将这些控件传递给readstream函数,但我不知道究竟是怎么回事。
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);
}
}
}
Before you call worker.RunWorkerAsync();
在调用
worker.RunWorkerAsync();
之前worker.RunWorkerAsync();
, do this: , 做这个:
string[] texts = new string[] {start, end};
worker.RunWorkerAsync(texts);
Then, in ReadStream(...)
然后,在
ReadStream(...)
string[] extracted = (string[])doWorkEventArgs.Argument;
string start = extracted[0];
string end = extracted[1];
Try this code to pass array as parameter: 尝试使用此代码将数组作为参数传递:
worker.RunWorkerAsync(array);
Use this code to get this array: 使用此代码获取此数组:
doWorkEventArgs.Argument
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.