[英]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. 我想在验证文件的同时显示一个等待的gif,这是我到目前为止尝试过的。
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. 有一个表单-Form1,用户单击按钮以验证文件,还有另一种表单-等待,只有一个显示gif图像的图片框。
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. VerifyFile()
完成后,如何自动关闭等待表单。
The problem with your code is that Form.ShowDialog() method is synchronous and it waits for the result from the "Form" dialog. 您的代码的问题在于Form.ShowDialog()方法是同步的,并且它等待“ Form”对话框的结果。 This means that the code execution is hold up untill the "wait" dialog will be closed. 这意味着在执行“等待”对话框将被关闭之前,将一直暂停执行代码。
Consider moving the VerifyFile() method into Wait dialog: 考虑将VerifyFile()方法移到“等待”对话框中:
class Wait: Form
{
public Wait() : base()
{
System.Threading.Tasks.Task.Factory.StartNew(() => VerifyFile());
}
}
You can close this dialog when after VerifyFile execute's over. VerifyFile执行结束后,可以关闭此对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.