Event Application.Exit () and FormClosing in Vb.net

I have one application for windows forms that runs in the icon in the system tray. If the user clicked the X button of the window, a message appears with the message “Yes” and “No” (“Yes” → close the form --- No-> save the form launched in the system tray icon). I thought to prevent the scenario when the user opens another instance of the application when the instance is already running, so I used this code:

 If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then 
 MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,
    MessageBoxIcon.Exclamation)
    Application.Exit()
End If

The problem is that when I want to test this message, a message is displayed, but after I click OK, a new message box will appear (this is from Private Sub Form_FormClosing). If I select NO, I will have to run the instance! I read that Application.Exit fires the Form_FormClosing event.

Is there any way to cancel the launch of the Form_FormClosing event, or am I something wrong?

'this is a form closing procedure

Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Try
        Dim response As MsgBoxResult
        response = MsgBox("Are you sure you want to exit", CType(MsgBoxStyle.Question + MsgBoxStyle.YesNo, MsgBoxStyle), "Confirm")

        'If the user press Yes the application wil close
        'because the application remains in taskmanager after closing i decide to kill the current process
        If response = MsgBoxResult.Yes Then
            Process.GetCurrentProcess().Kill()
        ElseIf response = MsgBoxResult.No Then
            e.Cancel = True
            Me.WindowState = FormWindowState.Minimized
            Me.Hide()
            NotifyIcon1.Visible = True
        End If

PS: I am not a programmer, so please do not be harsh with me :)

+5
source share
3 answers

You do not need to kill the current process or use the instruction End. If you need to use them, then something is wrong with your application.

, Me.Close. FormClosing:

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Case Windows.Forms.DialogResult.Yes
            'nothing to do here the form is already closing
        Case Windows.Forms.DialogResult.No
            e.Cancel = True 'cancel the form closing event
            'minimize to tray/hide etc here 
    End Select
End Sub

,

+5

, , VB End .

End Dispose Finalize Visual Basic. , , . End Try Catch, finally.

If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then  
   MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,         MessageBoxIcon.Exclamation) 
   End
End If 
+1
Private Sub main_master_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
If e.CloseReason = CloseReason.UserClosing Then
'Put you desired Code inside this! 
Msgbox("Application Closing from Taskbar") 
End If 
End Sub

It will close exe from the taskbar or kill the process. If the user closes the Application from the taskbar .

CloseReason.UserClosing 

event closes the application if it is closed by a user from Taskber

+1
source

All Articles