简体   繁体   中英

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

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

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.

You could then pass the BackgroundWorker instance to the background method.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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