我继承了BackgroundWorker并重写了OnDoWork:
protected override void OnDoWork(DoWorkEventArgs e)
{
WorkerEndedEvent.Reset();
base.OnDoWork(e);
}
我的问题是,它将从哪个线程调用?是否可以确保在我调用RunWorkerAsync()时调用它,还是可以在以后调用它(即从工作线程中调用)?
解决方法:
它将由工作线程调用,并且不能保证在调用RunWorkerAsync期间被调用.
RunWorkerAsync的文档指出:
The RunWorkerAsync method submits a request to start the operation
running asynchronously. When the request is serviced, the DoWork event
is raised, which in turn starts execution of your background
operation.
因此RunWorkerAsync仅提交启动操作的请求-这将引发DoWork事件.它不能保证何时调用DoWork.
它也是documented,因为它使用单独的线程来工作:
Executes an operation on a separate thread.
因此很明显,在不同于RunWorkerAsync调用的线程上调用DoWork.