I am using a background worker to complete a task. When the task is complete, I would like to lock the parent thread and show the user a message that indicates completion. I have simply tried this within the BackgroundWorker DoWork method using MessageBox.Show() which locks the thread its called from. But in this case the intent is to lock the parent thread. Unfortunately it appears that within the parent thread ,the call to RunWorkerAsync() is not waiting. Thus preventing me from checking completion after that statement. It also appears that awaiting RunWorkerAsync() and MessageBox.Show() is not possible.
private void Temp_Load(object? sender, EventArgs e)
{
complete = false;
_backgroundWorker.RunWorkerAsync();
//does not wait for RunWorkerAsync()
//MessageBox.Show("lock main ui thread");
}
private void backgroundWorker_DoWork(object? sender, DoWorkEventArgs e)
{
//does not lock parent thread
//MessageBox.Show("lock main ui thread");
complete = true;
}
Sandhiya PriyaPosted Sep 30, 2026, 6:18 AM
The main point is:
RunWorkerAsync()starts the background work; it does not wait for it to finish. If you want to show a message after the work is completed, use theRunWorkerCompletedevent.Simple understanding
Think of
BackgroundWorkerlike this:RunWorkerAsync()returns immediately, so this:does not mean "wait and then show the message."
It means:
Correct solution
Use
RunWorkerCompleted:Why
RunWorkerCompletedis importantDoWorkruns on a background thread.But
RunWorkerCompletedis called afterDoWorkfinishes and, in a normal WinForms application, executes on the UI thread.Therefore, you don't need to manually "lock the parent thread."
What about
MessageBox.Show()insideDoWork?You can technically do this:
But this displays the message from the background thread. It does not block/wait the UI thread.
The UI thread remains available while the background thread is showing the message.
If your requirement is:
Then
RunWorkerCompletedis the intendedBackgroundWorkerpattern.What happens with your original code?
Your code:
works approximately like:
The UI thread does not wait for
DoWork.That's why checking:
immediately after
RunWorkerAsync()won't work reliably.For example:
The result may still be:
because the background operation hasn't finished yet.
Fresher-friendly rule to remember
Method/Event
Runs where?
Purpose
RunWorkerAsync()UI thread
Starts background work
DoWorkBackground thread
Perform long-running operation
RunWorkerCompletedUI thread
Handle completion/result/error
MessageBox.Show()inRunWorkerCompletedUI thread
Show completion message
So the simple pattern is:
Don't try to make
RunWorkerAsync()wait. Its purpose is specifically to start asynchronous/background work. UseRunWorkerCompletedto continue the UI flow after the work has finished.Sam HobbsPosted Sep 29, 2026, 10:25 PM
I assume that parent thread is also the main ui thread.
I do not understand what you mean by lock the parent thread.
I will assume that the fundamental requirement is to show a message. The
BackgroundWorkerclass is designed to make things easy. You know thatRunWorkerAsynccausesDoWorkto execute in the other thread. If you just want to do something in the window, such as show a completion messge, then do that in theBackgroundWorker.RunWorkerCompletedevent handler. The technical details of execution in the UI thread is done for us, you can update a control, such as a text box, the same way that you update controls in other event handlers for the window .