Later, I need to send to the Loading Background Worker a signal that indicates that the Server Consult Background Worker has finished.
No you don't need to.
The background worker is exactly what it says: A "worker" that performs a task in the background. The task in your case is the method that communicates with the WCF service that you have. The worker will end when the work is done (or an exception is thrown). So the sequence of events is as follows:
- Start the worker (must be first if your form is modal)
- Show the form
- Listen for the worker
RunWorkerCompleted
event to hide the form
In fact I have created a form that does something like that, only I don't communicate to a server (doesn't matter actually) and I show a simple progress bar.
public partial class FastForwardForm : Form
{
private Exception asyncError;
public event DoWorkEventHandler DoWork
{
add { worker.DoWork += value; }
remove { worker.DoWork -= value; }
}
public FastForwardForm()
{
InitializeComponent();
}
public Exception AsyncError
{
get { return asyncError; }
}
private void FastForwardForm_Shown(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
statusLabel.Text = e.UserState.ToString();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null) asyncError = e.Error;
HideProgressForm();
worker.Dispose();
}
private void HideProgressForm()
{
//not actually needed, the callback runs on the UI thread.
if (InvokeRequired)
{
Invoke((Action)HideProgressForm);
return;
}
Close();
}
}
Please note the following:
- The
worker
is a Control
subclass, so I added it through the forms editor. That also means that its callbacks are executed on the UI thread and you don't need the if (InvokeRequired)
part that I added. Don't ask me why it's there, I missed it after a refactoring :)
- The form does some of the dirty work for you: starts the worker as soon as it's shown. Stores the exception of the process. Exposes the
DoWork
so that you can add the background work from anywhere outside the form.
- The code is not perfect: If you need to reuse the form, you will have to remove
worker.Dispose();
and add it to the forms Dispose()
method.
Now if you need to show some "waiting" animation in the form, you don't need another worker. You need a Timer control to refresh the animation, but that's another story.