I dont think you can tell if the app is killed by task manager. Maybe have the app write it is online with the time every 5 minutes. Have sql job or scheduled task that looks for users that are online but have not reported in for 15 minutes and change its status to offline
C# How do I detect user offline status? ( Task Manager )
I have developed a sample application, Mssql connection is available in this application.
I created a user table and defined the status field as a bool value.
I update status field when I login in the application. I added some close events for offline status.
But when I close it from Task manager, the program cannot detect it.
Example Events ;
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { MessageBox.Show("Closing Detected !"); }
How can I detect that the application is closing under all circumstances?
Thank you, Best Regards.
2 additional answers
Sort by: Most helpful
-
RLWA32 44,931 Reputation points
2022-02-14T10:36:09.097+00:00 Task Manager will first send a WM_CLOSE message to the application's window to give the application an opportunity to close in a controlled manner. If this is not handled by the application Task Manager then uses TerminateProcess to forcefully terminate the application.
Update:
Contrary to the documentation for FormClosingEventArgs.CloseReason Property the CloseReason property is set to UserClosing even when it is Task Manager that is ending the application.Perhaps you can set your own flag to differentiate when the application close was initiated by user action versus the Task Manager.
-
Erdem 21 Reputation points
2022-02-14T10:59:20.647+00:00 Thank you for answer, If I close the bottom Form1 it can be detected. But it cannot be detected when I try end task TestedClose.
Can you share me a example please ?