开发者

C# : OnClosing takes a while, how can I show a dialog with a marquee progressbar?

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-procedure开发者_开发技巧s and when that's done, close the progress-dialog.


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.


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();
     }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜