1

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

  • The answer to this can be found here – user4574834 Nov 05 '17 at 10:19
  • Why not just use a `Boolean` variable to indicate if the safety rules have been fired? When the app closes, check that variable and if it is `False` then don't allow the app to close. – Chris Dunaway Nov 06 '17 at 15:16
  • @ChrisDunaway Not bad idea but I saw that when Task Manager closes it doesn't follow any rules like `e.Cancel = True` because I added a messagebox in the formclosing event in which a user have to select yes to close and no to not but when the app closes from the task manager then it does show the messagebox but doesn't wait for the user action and forcefully closes the application. – Mohammed Julfikar Ali Mahbub Nov 06 '17 at 16:32
  • @user4574834 I checked the link you suggested but didn't worked for me! – Mohammed Julfikar Ali Mahbub Nov 06 '17 at 17:23

0 Answers0