If you do not want your code to execute asynchronously, do not put it in BackgroundWorker...
{
DoWork();
}
However, if there is some obscure reason why you absolutely must have the code in BackgroundWorker, you can use the following:
ManualResetEvent mre = new ManualResetEvent(false);
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += DoWork;
worker.RunWorkerCompleted += (s, e) =>
{
RunWorkerCompleted(s, e);
mre.Set();
};
// ...
worker.RunWorkerAsync();
mre.WaitOne();
source
share