C# How do I detect user offline status? ( Task Manager )

Erdem 21 Reputation points

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.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,493 questions
No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,811 Reputation points

    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

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. RLWA32 29,851 Reputation points

    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.

    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.

  2. Erdem 21 Reputation points


    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 ?