简体   繁体   English

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

[英]How can i get the instance of a BackgroundWorker from the currently executed method?

I am using a backgroundworker that can have n instances. 我正在使用可以有n个实例的后台工作程序。 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. 问题是DoWork方法(具有'sender'参数,即BackgroundWorker)调用产生回调的其他代码 - 因此我没有发件人。

How can i determine the BackgroundWorker that the current code is running under? 如何确定当前代码运行的BackgroundWorker?

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? 我确信这对BackgroundWorker很容易实现......任何想法?


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 我在我的描述中引起了一些困惑,这里有一些更多的事实:-) 1。)我已经调用了bw.RunWorkerAsync()2。)调用事件MethodCalledFromEventCallback的类不知道后台线程3.)我不能(由于设计要求)包括Backgroundworker作为参数

Thanks :-) 谢谢 :-)

BackgroundWorker.RunWorkerAsync(bw);

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
}

您可以将“发送者”作为参数发送到回调中,并以这种方式进入BGW吗?

You want to use the RunWorkerAsync method that takes an argument, which is then available as the Argument property of the event parameter. 您希望使用带有参数的RunWorkerAsync方法,该方法随后可用作事件参数的Argument属性。

You could then pass the BackgroundWorker instance to the background method. 然后,您可以将BackgroundWorker实例传递给后台方法。

Your design requirements need a bit of work. 您的设计要求需要一些工作。 A method that must use BackgroundWorker.UpdateProgress can't take the BackgroundWorker as a parameter? 必须使用BackgroundWorker.UpdateProgress方法不能将BackgroundWorker作为参数? 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. 如果你想要这种关注点分离,那么使用4.0中的Task库你可能会更开心。 They separate the work to be done (the "update" code) from the scheduling of that work. 他们将要完成的工作(“更新”代码)与该工作的安排分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM