简体   繁体   中英

Show modal loading window in Winforms for long-running process

I would like to show a wait gif, while verifying a file and this is what I have tried so far.
There is a form - Form1, where user clicks a button to verify a file, and there is another form - Wait, with just a picturebox showing a gif image.

button_click()
{
  Wait wait = new Wait();
  wait.ShowDialog();
  VerifyFile();
  wait.Close();
}

The Wait form does show up, but it doesn't close. Also, the verification is also not done. It continues only when I manually close the Wait form. How to auto close the wait form, once VerifyFile() is complete.

The problem with your code is that Form.ShowDialog() method is synchronous and it waits for the result from the "Form" dialog. This means that the code execution is hold up untill the "wait" dialog will be closed.

Consider moving the VerifyFile() method into Wait dialog:

class Wait: Form
{
    public Wait() : base() 
    {
        System.Threading.Tasks.Task.Factory.StartNew(() => VerifyFile());
    }
}

You can close this dialog when after VerifyFile execute's over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM