【发布时间】:2010-07-12 21:53:47
【问题描述】:
我正在使用一个可以有 n 个实例的后台工作程序。问题是 DoWork 方法(具有“sender”参数,即 BackgroundWorker)调用其他产生回调的代码 - 因此我没有发送者。
如何确定当前代码运行的 BackgroundWorker?
例如:
private void SetupThread()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(DoTheWork);
}
private void DoTheWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// I know that sender here can be converted, but thats no good for me
MyClass.DoSomething(); // This will produce a callback(event) from MyClass
}
private void MethodCalledFromEventCallback()
{
// Here is where the actual work is being done. I need to determine which
// instance of the Backgroundworker is calling it so that i can use the
// UpdateProgress method
// I cannot do this :-( (BackgroundWorker)System.Threading.Thread.CurrentThread;
}
我可能只是忽略了一些东西(除非线程池是有序的:-()
我确定这可以通过 BackgroundWorker 轻松实现...有什么想法吗?
编辑
我的描述引起了一些混乱,这里有更多事实:-) 1.) 我已经调用了 bw.RunWorkerAsync() 2.) 调用事件 MethodCalledFromEventCallback 的类不知道后台线程 3.) 我不能(由于设计要求)将 Backgroundworker 包含为参数
谢谢:-)
【问题讨论】:
标签: c# multithreading backgroundworker