akinuri
akinuri

Reputation: 12029

Show a form when another form is closed

On the click of a button (that's in the main form), I show a (second) form that does some parsing (decompress, extract, get information, etc) of an archive file. After this process is done, I close the second (parsing) form and show another (third) form that contains the parsed information from the archive.

Click callback looks like this:

private void ParseInputBackupButton_Click(object sender, EventArgs e)
{
    Form PBF = new ParseBackupForm();
    Form CBF = new CreateBackupForm();
    PBF.FormClosed += delegate
    {
        CBF.ShowDialog();
    };
    PBF.ShowDialog();
}

and the second form:

private void ParseBackupForm_Load(object sender, EventArgs e)
{
    new Thread((ThreadStart)delegate
    {

        // parse and update form

        this.Invoke((MethodInvoker)delegate
        {
            this.Close();
        });

    }).Start();
}

The problem is when the third form (CreateBackupForm) appears, the second form (ParseBackupForm) doesn't close. They both appear. If I don't show the third form, the second form closes.

screenshot of the forms

What am I doing wrong?

Upvotes: 0

Views: 62

Answers (3)

iajs
iajs

Reputation: 71

As you are calling ShowDialog on both windows, couldn't you just call them one after the other?

i.e.

PBF.ShowDialog();
CBF.ShowDialog();

The second call will not be made until the PBG dialog has closed.

Edit: The reason why the second form doesn't close is that the you are subscribing to an event raised by the form as it closes, then within that delegate calling ShowDialog which blocks the form from actually closing.

Upvotes: 1

XouDo
XouDo

Reputation: 975

You could BeginInvoke the call of CBF.ShowDialog();, thus not blocking the completion of the FormClosed callback method and the closing of the PBF form.

Upvotes: 0

Gibbon
Gibbon

Reputation: 2773

As far as I can remember .ShowDialog() is a blocking method, so would stop the other form from completing its close method until that form is also closed?

Pretty sure you can use .Show() to just make a form visible without blocking?

Upvotes: 1

Related Questions