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

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

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 }.

The final block that actually performs the work is from an event of a class, it has no knowledge of the BackgrounWorker :-( – schmoopy Jul 12 '10 at 22:35.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions