Know if the application has been completed by the task manager

Asked

Viewed 325 times

4

There’s no way my app knows it’s being terminated by the task manager?

If the user is going to finish my application by task manager, my application detects this completion and performs some tasks before finishing?

How can I do that?

My application is running straight and is in WPF.

2 answers

5

It is possible to check through the Formclosingeventargs Event.

See the example below. It’s working in my application.

    private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.TaskManagerClosing)
        {
            MessageBox.Show("Fechou pelo gerenciador de tarefas...");
        }
    }

View documentation.

https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx

  • 1

    This only works when the termination is done in a specific way. And only in Winforms, not in WPF.

  • For me it does not work because my application is in wpf, and in wpf the event is CancelEventArgs

4


There is no reliable way to do this. The user can always finish the way he wants.

In some situations it is possible to capture a Windows message and do something. But there are no guarantees that this message will be sent by Windows. It depends on how the user is finishing the process. It doesn’t even need to be by the task manager. It has a article about this from Master Raymond Chen.

Take a look at this documentation from Process.WaitForExit(), can help you but I have doubts.

Ask the OS about the subject.

In short, forget about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.