发送参数到后台工作?

比方说,我想发送一个int参数给后台工作人员,这怎么能完成呢?

private void worker_DoWork(object sender, DoWorkEventArgs e) { } 

我知道什么时候这是worker.RunWorkerAsync();我不明白如何在worker_DoWork中定义它应该采取一个int参数。

你开始像这样:

 int value = 123; bgw1.RunWorkerAsync(value); // 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. } 

尽pipe这已经是一个已经回答的问题,但我还是会留下另外一个select,那就是IMO更容易阅读:

 BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (obj, e) => WorkerDoWork(value, text); worker.RunWorkerAsync(); 

并在处理程序的方法:

 private void WorkerDoWork(int value, string text) { ... } 

你可以像这样传递多个参数。

 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. } 

您可以使用DoWorkEventArgs.Argument属性。

一个完整的例子(甚至使用一个int参数)可以在微软的网站上find:

  • 如何:在后台运行一个操作

查看DoWorkEventArgs.Argument属性 :

 ... 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; } } 

您需要RunWorkerAsync(object)方法和DoWorkEventArgs.Argument属性。

 worker.RunWorkerAsync(5); private void worker_DoWork(object sender, DoWorkEventArgs e) { int argument = (int)e.Argument; //5 } 

您应该总是尝试使用具体types的复合对象(使用复合devise模式),而不是使用对象types列表。 谁记得那些对象是什么? 稍后考虑维护你的代码…而是尝试如下所示:

 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; } 

如果要传递多个types的参数,则可以尝试此操作,首先将它们全部添加到Objecttypes的数组中,并将该对象传递给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...... //..... }