C#:OnClosing 需要一段时间,如何显示带有选取框进度条的对话框?

发布于 2024-08-14 05:54:30 字数 141 浏览 3 评论 0原文

我必须在 OnClosing 处理程序中进行大量清理,这使得我的应用程序看起来像是挂起的。

如何显示带有字幕式进度条的小对话框,以便用户至少知道它仍在“工作”?

基本上我想开始对话, 继续执行我所有的关闭程序,完成后, 关闭进度对话框。

I have to do quite some cleanup in my OnClosing handler, which makes it seem like my app is hanging.

How can I show a small dialog with a marquee-style progressbar so users will at least know it's still 'working' ?

Basicly I want to start the dialog,
proceed with all my shutdown-procedures and when that's done,
close the progress-dialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-08-21 05:54:30

有很多方法可以实现此目的,但其中一种可能如下所示:

protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    Form dlg=null;
    ThreadPool.QueueUserWorkItem(state => {
        dlg = new ShuttingDownUI();
        dlg.ShowDialog();
    });

    // do hard work with saving and stuff

    if (dlg != null)
    {
        dlg.BeginInvoke((Action) dlg.Close);
    }
}

在表单的 OnClosing 方法中,在包含 ProgressBar 及其 Style 的单独线程上打开一个“关闭对话框” 属性设置为 Marquee。然后继续执行保存/关闭程序,完成后关闭“关闭”对话框。

重要的是,带有选取框的表单必须位于与正在完成工作的线程不同的线程上。否则它不会产生动画,并且应用程序仍然会显得没有响应。

There are many ways to achieve this, but one could looke like this:

protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    Form dlg=null;
    ThreadPool.QueueUserWorkItem(state => {
        dlg = new ShuttingDownUI();
        dlg.ShowDialog();
    });

    // do hard work with saving and stuff

    if (dlg != null)
    {
        dlg.BeginInvoke((Action) dlg.Close);
    }
}

In the OnClosing method of your form, open a "shutting down dialog" on a separate thread containing a ProgressBar with its Style property set to Marquee. Then go on and do your saving/closing down procedure, and when you are done, close the "shutting down" dialog.

The important thing is that the form with the marquee must be on a separate thread than the one where the work is being done. Otherwise it will not animate, and the app will still appear as if it is not responding.

阳光下慵懒的猫 2024-08-21 05:54:30

你有没有尝试过类似的事情:

  private void Form1_FormClosing( object sender, FormClosingEventArgs e )
  {
     using ( Form2 myForm2 = new Form2() )
     {
        myForm2.Show();

        //Do your stuff here

        myForm2.Close();
     }
  }

Have you tried something like:

  private void Form1_FormClosing( object sender, FormClosingEventArgs e )
  {
     using ( Form2 myForm2 = new Form2() )
     {
        myForm2.Show();

        //Do your stuff here

        myForm2.Close();
     }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文