C # – How to get an instance of BackgroundWorker from the currently executed method?

I’m using a background worker program that can have n instances. The problem is that the DoWork method (with the’sender’ parameter, that is, BackgroundWorker) calls other codes that generate the callback-so I didn’t send the message People.

How to determine the BackgroundWorker that the current code is running?

For example:

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;

}

I may just overlook something (unless the thread pool is in order:-()

I am sure this is easy to implement for BackgroundWorker… Any ideas?

Edit

I caused some confusion in my description, here are some more facts 🙂
1.)I have called bw.RunWorkerAsync()
2.) Call things The class of the MethodCalledFromEventCallback does not know the background thread
3.) I cannot (due to design requirements) include Backgroundworker as a parameter

Thank you:-)

As far as I know, the best way to use background workers is probably (assuming the constraints you 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< br />)

I am using a background worker that can have n instances. The problem is that the DoWork method (with the’sender’ parameter, that is, the BackgroundWorker) call generates a callback Other code-so I don’t have a sender.

How to determine the BackgroundWorker that the current code is running on?

For example:

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;

}

I may just overlook something (unless the thread pool is in order:-()

I am sure this is easy to implement for BackgroundWorker… Any ideas?

Edit

I caused some confusion in my description, here are some more facts 🙂
1.)I have called bw.RunWorkerAsync()
2.) Invoke the event Method The class of CalledFromEventCallback does not know about background threads.
3.) I cannot (due to design requirements) include Backgroundworker as a parameter

Thank you:-)

As far as I know, the best way to use background workers is probably (assuming the constraints you 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 = () => < br /> {
((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
}

Leave a Comment

Your email address will not be published.