Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show wait message while application exit

Tags:

c#

winforms

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, Application 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

like image 956
Bhaskar Avatar asked Mar 20 '26 05:03

Bhaskar


2 Answers

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

like image 94
Akram Shahda Avatar answered Mar 21 '26 18:03

Akram Shahda


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:

enter image description here

  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).

like image 44
Teoman Soygul Avatar answered Mar 21 '26 17:03

Teoman Soygul