The task.
I have one method which runs two long-time functions inside.
Im not talking about locking objects here. These funcs are independent, only have same parameter which used in readonly.
I want one func to run inside thread.
private static ManualResetEvent mre = new ManualResetEvent(false);
private Result result;
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync(new BackgroundWorkerArgument(bitmap, image));
Rectangle rectangle = DetectRectangle(bitmapClone));
mre.WaitOne();
mre.Reset();
<another code, dont bother>
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
result= (Result)e.Result;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorkerArgument argument = (BackgroundWorkerArgument)e.Argument;
e.Result = CreateResult(argument.Bitmap, argument.Image);
mre.Set();
}
The problem.
<another code, dont bother>
runs immediately after i do Set()
. Its ok.
But bw_RunWorkerCompleted
runs after that. This means result variable has null, but i want it contains the result of thread's function.
How to rework my code - result= (Result)e.Result
must runs before next line to WaitOne()
?
If its not possible with BackgroundWorker
maybe i need to use threads ?
I'm using .NET 2.0.