Question

I have an application that when busy will open a busy form (FormWaitingForm) to indicate to the user that the application is busy. How do i close FormWaitingForm in the event backgroundWorker1_RunWorkerCompletedbelow ?

private void radButtonCheckFiles_Click(object sender, EventArgs e)
        {
                    var bw = new BackgroundWorker();

                    // define the event handlers
                    bw.DoWork += new DoWorkEventHandler(ProcessTickTemp);

                    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

                    bw.RunWorkerAsync(); // starts the background worker

                    // execution continues here in parallel to the background worker


                    using (var FormWaitingForm = new WaitingForm()) //  
                    {
                        var result = FormWaitingForm.ShowDialog();

                    }
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
             // How do i close FormWaitingForm here ?
        }
Was it helpful?

Solution

You could try something like this. Retain a reference to the form outside of the click method and then open it non-modally (so that you don't have to wait for the user to close it).

WaitingForm formWaitingForm;

private void radButtonCheckFiles_Click(object sender, EventArgs e)
{
    // background code here

    formWaitingForm = new WaitingForm();
    formWaitingForm.Show();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    formWaitingForm.Close();
    formWaitingForm.Dispose();
}

You would have to add some code to handle if the user closes the waiting form without waiting for you to do it.

That said, the way I usually implement a waiting/progress-type form is to incorporate the background process into the progress form itself and show something like a progress bar.

This link might give you some more ideas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top