In my application there are some safety rules to shutdown which fires when the user close the application normally and not by Task Manager. I tried the following code to detect the form is closing by TaskManager,
Private Sub MainInterface_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.TaskManagerClosing Then
e.Cancel = True
MsgBox("You must follow the safety procedure to shutdown the application")
End If
End Sub
But this didn't work so I decide to check actually what is the reason the application is getting while shutdown by TaskManager. So I tried,
Private Sub MainInterface_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.TaskManagerClosing Then
MsgBox(e.CloseReason)
End If
End Sub
And the result I got is 3, which means it is being closed with reason 'UserClosing'.
Indexes of e.CloseReasons
are:
(0) - CloseReason.None
(1) - CloseReason.WindowsShutDown
(2) - CloseReason.MdiFormClosing
(3) - CloseReason.UserClosing
(4) - CloseReason.TaskManagerClosing
(5) - CloseReason.FormOwnerClosing
(6) - CloseReason.ApplicationExitCall