如何从当前执行的方法中获取BackgroundWorker的实例?
我正在使用一个可以有 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作为参数,
谢谢:-)
I am using a backgroundworker that can have n instances. Problem is the DoWork method (that has the 'sender' parameter which is the BackgroundWorker) calls other code that produces a callback - thus i dont have the sender.
How can i determine the BackgroundWorker that the current code is running under?
ex:
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;
}
Im probably just overlooking something (unless threadpool is in order :-( )
Im sure this is easily doable with the BackgroundWorker ... any ideas?
edit
I caused some confusion in my description, here are some more facts :-)
1.) I already call the bw.RunWorkerAsync()
2.) The class that invokes the event MethodCalledFromEventCallback has no knowledge of the background thread
3.) I cannot (due to design requirements) include the Backgroundworker as a parameter
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知,您可以使用后台工作人员的最好方法是(假设您到目前为止提到的约束):
As far as I know the best you could probably get away with using a background worker is (assuming the constrants you've mentioned so far):
您能否将“发送者”作为参数发送给回调,然后以这种方式到达 BGW?
Could you just send the "sender" along as a parameter to the callback, and get at the BGW that way?
您想要使用带有参数的 RunWorkerAsync 方法,该方法是然后可用作 Argument 属性事件参数。
然后,您可以将BackgroundWorker 实例传递给后台方法。
You want to use the RunWorkerAsync method that takes an argument, which is then available as the Argument property of the event parameter.
You could then pass the BackgroundWorker instance to the background method.
您的设计要求需要做一些工作。 必须使用
BackgroundWorker.UpdateProgress
的方法不能将BackgroundWorker
作为参数吗?我想,您可以修改戈登的答案以接受代表。如果您想要这种关注点分离,那么您可能会更乐意使用 4.0 中的
Task
库。他们将要完成的工作(“更新”代码)与该工作的调度分开。Your design requirements need a bit of work. A method that must use
BackgroundWorker.UpdateProgress
can't take theBackgroundWorker
as a parameter? You could modify Gordon's answer to take a delegate instead, I suppose.If you want this kind of separation of concerns, then you'll probably be a lot happier using the
Task
library in 4.0. They separate the work to be done (the "update" code) from the scheduling of that work.