如何从当前执行的方法中获取BackgroundWorker的实例?

发布于 2024-09-09 07:34:58 字数 1129 浏览 5 评论 0原文

我正在使用一个可以有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一杆小烟枪 2024-09-16 07:34:58

据我所知,您可以使用后台工作人员的最好方法是(假设您到目前为止提到的约束):

private void SetupThread()
{
    BackgroundWorker bw = new BackgroundWorker();
    // Assuming you need sender and e. If not, you can just send bw
    bw.DoWork += new DoWorkEventHandler(DoTheWork);
}

private void DoTheWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    MyClass.Callback = () => 
        {
            ((BackgroundWorker)bw).UpdateProgress(/*send your update here*/);
            MethodCalledFromEventCallback();
        };

    MyClass.DoSomething(); // This will produce a callback(event) from MyClass
}

private void MethodCalledFromEventCallback() 
{ 
    // You've already sent an update by this point, so no background parameter required
}

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):

private void SetupThread()
{
    BackgroundWorker bw = new BackgroundWorker();
    // Assuming you need sender and e. If not, you can just send bw
    bw.DoWork += new DoWorkEventHandler(DoTheWork);
}

private void DoTheWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    MyClass.Callback = () => 
        {
            ((BackgroundWorker)bw).UpdateProgress(/*send your update here*/);
            MethodCalledFromEventCallback();
        };

    MyClass.DoSomething(); // This will produce a callback(event) from MyClass
}

private void MethodCalledFromEventCallback() 
{ 
    // You've already sent an update by this point, so no background parameter required
}
野の 2024-09-16 07:34:58
BackgroundWorker.RunWorkerAsync(bw);
BackgroundWorker.RunWorkerAsync(bw);
浪推晚风 2024-09-16 07:34:58

您能否将“发送者”作为参数发送给回调,然后以这种方式到达 BGW?

Could you just send the "sender" along as a parameter to the callback, and get at the BGW that way?

︶葆Ⅱㄣ 2024-09-16 07:34:58

您想要使用带有参数的 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.

獨角戲 2024-09-16 07:34:58

您的设计要求需要做一些工作。 必须使用BackgroundWorker.UpdateProgress的方法不能将BackgroundWorker作为参数吗?我想,您可以修改戈登的答案以接受代表。

如果您想要这种关注点分离,那么您可能会更乐意使用 4.0 中的 Task 库。他们将要完成的工作(“更新”代码)与该工作的调度分开。

Your design requirements need a bit of work. A method that must use BackgroundWorker.UpdateProgress can't take the BackgroundWorker 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文