Answers:
您可以这样启动:
int value = 123;
bgw1.RunWorkerAsync(argument: value); // the int will be boxed
然后
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
int value = (int) e.Argument; // the 'argument' parameter resurfaces here
...
// and to transport a result back to the main thread
double result = 0.1 * value;
e.Result = result;
}
// the Completed handler should follow this pattern
// for Error and (optionally) Cancellation handling
private void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
// check error, check cancel, then use result
if (e.Error != null)
{
// handle the error
}
else if (e.Cancelled)
{
// handle cancellation
}
else
{
double result = (double) e.Result;
// use it on the UI thread
}
// general cleanup code, runs when there was an error or not.
}
Tuple<A,B>
(C#4 +)(编辑:是,使用对象将其全部打包。例如,参见DoWorkEventArgs self)。
label1.Text = e.Result.ToString();
到处我都将其标记为安全。
尽管这是一个已经回答的问题,但我还是会提出另一个选择,即IMO更容易阅读:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => WorkerDoWork(value, text);
worker.RunWorkerAsync();
并在处理程序方法上:
private void WorkerDoWork(int value, string text) {
...
}
... => WorkerDoWork(a, b, c);
,只要它与方法签名匹配... WorkerDoWork(int a, string b, string c) {...
您可以像这样传递多个参数。
List<object> arguments = new List<object>();
arguments.Add(argument 1);
arguments.Add(argument 1);
arguments.Add(argument n);
backgroundWorker2.RunWorkerAsync(arguments);
private void worker_DoWork(object sender, DoWorkEventArgs e) {
List<object> genericlist = e.Argument as List<object>;
extract your multiple arguments from this list and cast them and use them.
}
Tuple
(或专门的类)而不是一个通用对象列表
...
backgroundWorker1.RunWorkerAsync(yourInt);
...
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;
// Extract the argument.
int arg = (int)e.Argument;
// Start the time-consuming operation.
e.Result = TimeConsumingOperation(bw, arg);
// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
如果您想传递不止一种类型的参数,可以尝试一下,首先将它们全部添加到Object类型的数组中,然后将该对象传递给RunWorkerAsync(),这是一个示例:
some_Method(){
List<string> excludeList = new List<string>(); // list of strings
string newPath ="some path"; // normal string
Object[] args = {newPath,excludeList };
backgroundAnalyzer.RunWorkerAsync(args);
}
现在在后台工作程序的doWork方法中
backgroundAnalyzer_DoWork(object sender, DoWorkEventArgs e)
{
backgroundAnalyzer.ReportProgress(50);
Object[] arg = e.Argument as Object[];
string path= (string)arg[0];
List<string> lst = (List<string>) arg[1];
.......
// do something......
//.....
}
您需要RunWorkerAsync(object)方法和DoWorkEventArgs.Argument属性。
worker.RunWorkerAsync(5);
private void worker_DoWork(object sender, DoWorkEventArgs e) {
int argument = (int)e.Argument; //5
}
您应该始终尝试使用具有具体类型的复合对象(使用复合设计模式),而不是使用对象类型列表。谁会记得这些对象到底是什么?稍后考虑考虑代码的维护...而是尝试如下操作:
Public (Class or Structure) MyPerson
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public int ZipCode { get; set; }
End Class
然后:
Dim person as new MyPerson With { .FirstName = “Joe”,
.LastName = "Smith”,
...
}
backgroundWorker1.RunWorkerAsync(person)
然后:
private void backgroundWorker1_DoWork (object sender, DoWorkEventArgs e)
{
MyPerson person = e.Argument as MyPerson
string firstname = person.FirstName;
string lastname = person.LastName;
int zipcode = person.ZipCode;
}