Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue executing code after calling ShowDialog()

Tags:

the Form.ShowDialog() method causes the code to be halted until the newly called form is closed. I need the code to continue running after the ShowDialog() method is called. I googled and read about using backgroundworker? But that is the first time i have heard of that and never used it before.

Form2 form2this = new Form2();
form2this.ShowDialog();
MessageBox.Show("Something");

This code gets executed after clicking a button, how can i still call ShowDialog to prevent the user from interacting with the main form but still allowing the main form to continue with its work?

Sorry if its been discussed but everything i found seems extremely difficult to perform such a simple task. I am actually surprised its not included in the SHowDialog method. for instance ShowDialog().Continue would be cool.

like image 1000
Seth Avatar asked Nov 05 '12 19:11

Seth


People also ask

How to continue executing code after calling ShowDialog() in c#?

If you just want the code to continue on instead of blocking until the popup is closed consider using Show instead of ShowDialog .

What happens after a form that has been displayed with the ShowDialog method is closed by the user?

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.

What is the difference between show and ShowDialog?

Show() method shows a windows form in a non-modal state. ShowDialog() method shows a window in a modal state and stops execution of the calling context until a result is returned from the windows form open by the method.

What is ShowDialog?

ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window. Modal windows are primarily used as dialog boxes.


2 Answers

  • If you just want the code to continue on instead of blocking until the popup is closed consider using Show instead of ShowDialog.

  • If you have some action that you want to have the parent form doing while the child form is up, then yes, it could be appropriate to use a BackgroundWorker (or just manually starting a new Thread/Task). It would be helpful to know more about what that task is though. If you need to interact with the main form, or the child form, then that seems like trouble to me; if you just need to do some background task with no UI interaction then this is the right line of thought.

  • Another possibility is that what you want to do really just should be something done in the child form, rather than the parent form.

like image 111
Servy Avatar answered Sep 21 '22 15:09

Servy


As long as you do asynchronous operations during the time that the modal dialog is opened, you can do it as simply as shown below, assuming button1_Click() is the event handler for a button.

private async void button1_Click(object sender, EventArgs e)
{
    // create and display modal form
    Form2 modalForm = new Form2();
    BeginInvoke((Action)(() => modalForm.ShowDialog()));

    // do your async background operation
    await DoSomethingAsync();

    // close the modal form
    modalForm.Close();
}


private async Task DoSomethingAsync()
{
    // example of some async operation....could be anything
    await Task.Delay(10000);
}

I found that when I used the solution that suggested to use Show(), I could end up in cases where the dialog I wanted to be modal would end up behind the main form, after switching back and forth between apps. That never happens when I use the solution above.

like image 26
Joe Savage Avatar answered Sep 21 '22 15:09

Joe Savage