在 C# 运行时创建事件处理程序
我正在创建一个BackgroundWorker数组,它共享一个事件处理程序,如下所示:
BackgroundWorker[] workers = new BackgroundWorker[files.length];
for(int i = o; i<files.length; i++)
{
workers[i] = new BackgroundWorker();
workers[i].DoWork += new DoWorkEventHandler(worker_DoWork);
workers[i].RunWorkerCompleted += newRunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
workers[i].RunWorkerAsync(files[i]);
}
所有工作人员都共享相同的事件处理程序,该事件处理程序仅使用不同的参数和结果执行相同的操作,如下所示:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeSomething(e.Argument.ToString());
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
resultArray.Add(e.Result);
}
private int ComputeSomething(string file)
{
...
return number;
}
显然在代码中,我正在尝试制作一个列表异步运行的BackgroundWorker,但当我检查结果时,其中一些不正确。我猜测“e.result”的值被其他工作人员替换,因为他们共享相同的事件处理程序,如果是这种情况,那么我想为每个BackgroundWorker创建单独的事件处理程序,以便e.result的值不会被替换。我该怎么做?
I'm creating an array of BackgroundWorker that shares one event handler like this:
BackgroundWorker[] workers = new BackgroundWorker[files.length];
for(int i = o; i<files.length; i++)
{
workers[i] = new BackgroundWorker();
workers[i].DoWork += new DoWorkEventHandler(worker_DoWork);
workers[i].RunWorkerCompleted += newRunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
workers[i].RunWorkerAsync(files[i]);
}
All workers are sharing the same event handler which does same thing only with different argument and result like this:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeSomething(e.Argument.ToString());
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
resultArray.Add(e.Result);
}
private int ComputeSomething(string file)
{
...
return number;
}
obviously in the code, I'm trying to make a list of BackgroundWorker that runs asyncronously but when I checked the results, some of them are incorrect. I'm guessing that the value of "e.result" were replaced by other workers since they share the same event handler if that is the case then i would like to create Individual event handlers for each BackgroundWorker so that the value of e.result won't get replaced. How will i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试同步对 resultArray 的访问:
Try synchronizing the access to resultArray:
共享相同的事件处理程序方法应该不是问题 - 它是
BackgroundWorker
提供的“e”,您可以在其中存储结果。另一方面,您从多个线程访问
resultArray
。这可能会引起问题。您在结果中看到什么样的错误?除了最后合并结果的方式之外,我希望它没问题。Sharing the same event handler method shouldn't be a problem - it's the
BackgroundWorker
which provides the "e" in which you store the result.On the other hand, you are accessing
resultArray
from multiple threads. That could be causing a problem. What sort of errors are you seeing in the results? Other than the way you're combining the results at the end, I'd expect it to be okay.我不明白 e.Result 如何被其他工作人员取代。
当您说某些结果不正确时,您的意思是该值是错误的,根本没有价值,或者可能某些结果是重复的,而另一些结果却消失了?
在我看来,在添加到 resultsArray 时应该放置一个同步锁,以确保它是线程安全的。
I don't see how e.Result could be replaced by other workers.
When you say some results are incorrect, do you mean the value is wrong, that there's no value at all, or maybe some results are duplicated while others disappear?
It seems to me you should put a synclock when adding to resultsArray to make sure it's thread-safe.