The following code adds numbers from 1 to 100 and returns the sum. What I'm trying to do is run the calculations in the workworker and return the value. The problem is that returnValue returns before DoWork completes. How can I wait for my background to finish before returning a value? (It seems I can't return the return to my DoWork ...)
double returnValue = 0;
var b = new BackgroundWorker();
b.DoWork += new DoWorkEventHandler(
delegate(object sender, DoWorkEventArgs e) {
for(int i=0;i<100;i++){
returnValue += (i+1);
}
}
);
b.RunWorkerAsync();
return returnValue;
Addendum: Would it be better to send a message pump in the same thread instead of running it on the background desktop?
Also, this is just a sample code, my actual code takes more than a minute.
source
share