question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked simonberrurier-7045 commented

C# Winform: How to capture win application closing from task manager

I have winform application or console application. now i want to capture if user close application clicking on cross button or close application from task manager.

before closing i want to send a mail and after mail sent application will be close. how could i capture winform or console application terminating from task bar?

please guide me how to do it by a sample code.

Thanks

dotnet-csharpwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can hook to events like FormClosing and take some actions. However, if the user kills the process from Task Manager, such events won't raise.

0 Votes 0 ·
DanielZhang-MSFT avatar image
1 Vote"
DanielZhang-MSFT answered simonberrurier-7045 commented

Hi TZacks-2728,
The task manager calls the Windows API ExitProcess to kill the process.
For WinForms applications, you can try Application.ApplicationExit event which occurs when the application is about to shut down.
You must attach an event handler to the ApplicationExit event to send mail before the application stops running.

  // Handle the ApplicationExit event to know when the application is exiting.
     Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

For more details, you can refer to the example in the document.
And the code example using the forked process in the link provided by periczeljkosmederevo is also a good suggestion.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your guide line.

winform application has this event Closed & Closing

 form1 = new AppForm1();
     _form1.Closed += new EventHandler(OnFormClosed);
     _form1.Closing += new CancelEventHandler(OnFormClosing);

but when some one working on console application then how to capture user has clicked on cross button ?

please advise. thanks

0 Votes 0 ·

you marked the question answered, this means your problem is solved

1 Vote 1 ·
periczeljkosmederevo avatar image
1 Vote"
periczeljkosmederevo answered Castorix31 commented

Hello,
there are event handlers, FormClosing and FormClosed, that are raised when Main form is closing or is closed. Trough FormClosingEventArgs e variable and its property e.ClosingReason You can find out why app is closing. Both events are raised when user click at close button on window form, the X button, or at task bar selects option end task, or close button.

 void Main_Form_Closing(object sender, FormClosingEventArgs e)
 {
        MessageBox.Show(e.CloseReason.ToString());
 }

Here is good article about that subject:

https://www.codeproject.com/articles/20533/how-to-know-when-the-task-manager-kills-your-app

All the best,
Željko Perić

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This does not work on Windows 10
Closing by Task Manager can be detected with WM_SYSCOMMAND

1 Vote 1 ·