开发者

Show wait message while application exit

开发者 https://www.devze.com 2023-03-08 15:00 出处:网络
I have the following code, Some times ArchiveFiles() take more time to complete its execution. When user clicks exit option more than one time from context menu, A开发者_如何学编程pplication becomes

I have the following code, Some times ArchiveFiles() take more time to complete its execution.

When user clicks exit option more than one time from context menu, A开发者_如何学编程pplication becomes non responding if ArchiveFiles() takes more time. How can I show a wait message when he clicks the exit option again?

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    ArchiveFiles(); 
    Application.Exit();
}

Thanks

Bhaskar


You can use BackgroundWorker.

Using BackgroundWorker, you will be able to execute time consuming tasks such as the one you have on another thread so that the UI doesn't freeze. You will be able to report the progress of that task, then report it accomplishement and executing what ever logic you need after its completion.

Handle the BackgroundWorker.DoWork event to start the operation that performs the potentially time-consuming work.

Handle BackgroundWorker.ProgressChanged event to report the progress of an asynchronous operation to the user.

Finaly, handle BackgroundWorker.RunWorkerCompleted event to implement whatever logic you want to be implemented after the task has been completed.


Refer to the following:

BackgroundWorker Component Overview

C# BackgroundWorker Tutorial


Create a new WaitingForm and put an image control on the form and use the below .gif in that image control which which automatically animate. Then use the code below:

Show wait message while application exit

  private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  {
     using (var wait = new WaitingForm())
     {
       wait.Show();
       ArchiveFiles(); 
       Application.Exit();
     }
  }

This may not be the best solution but it is the quickest. If you want the form to be a dialog, use wait.ShowDialog(); instead and carry ArchiveFiles(); Application.Exit(); functions inside it (if that is a probability).


You'd have to run ArchiveFiles asynchronously, by either using a thread or task, or making some delegate and using its BeginInvoke method.

You should use then some synchronization object like ManualResetEvent so the main thread don't continue executing, calling Application.Exit(). Or you could use some event to know when the operation finishes and then call Application.Exit() (or call it inside ArchiveFiles), but I find that to be worse.

0

精彩评论

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