'End Task' in Task Manager always sets CloseReason.UserClosing

后端 未结 2 948
再見小時候
再見小時候 2021-01-18 04:29

I want to log if a customer tries to force close the application. I\'m aware of having no chance to catch a process kill. But it should be possible through the main form clo

2条回答
  •  轮回少年
    2021-01-18 05:05

    Another solution for determining when the Task Manager is closing the program is by checking if the main form, or any of its controls, has focus. When you close via the Task Manager, the application is not focused, whereas if you close via the close button or Alt+F4, the application does have focus. I used a simple if check:

    private void MyForm_Closing(object sender, FormClosingEventArgs e)
    {
        if (this.ContainsFocus)
        {
            // Put user close code here
        }
    }
    

提交回复
热议问题