简体   繁体   English

在Winforms中显示模式加载窗口以长时间运行

[英]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.

相关问题 显示长时间运行过程的进度 - Show progress on long-running process 在长时间运行过程中禁用鼠标点击? - Disable mouse clicks during long-running process? 返回之前,先使用DBContext Access启动长时间运行的进程 - Start Long-Running Process With DBContext Access Before Returning IIS进程之外的长期运行的存储过程 - Long-running stored procedure out of IIS process 关闭Windows服务中的长时间运行的进程 - Shutting down a Long-running process in a Windows Service 在长时间运行的任务中显示自定义对话框窗口 - Displaying a custom dialog window during a long-running task 如何在C#Winforms应用程序中取消长时间运行的异步任务的执行 - How to cancel execution of a long-running async task in a C# Winforms app 在长时间运行的过程中,使用ReactiveUI,Observables,SubscribeOn和ObserveOn在UI中显示输出日志 - Using ReactiveUI, Observables, SubscribeOn, and ObserveOn to display an output log in UI during a long-running process 如何防止NHibernate长时间运行的进程锁定网站? - How to prevent NHibernate long-running process from locking up web site? 显示任何过程,如模态窗口 - Show any Process like Modal Window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM