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

Erdem 21 Reputation points
2022-02-14T08:33:56.423+00:00

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.

C#
C#
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.
10,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points
    2022-02-14T10:02:21.687+00:00

    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 40,756 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.

    0 comments No comments

  2. Erdem 21 Reputation points
    2022-02-14T10:59:20.647+00:00

    174046-image.png

    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 ?