Vb.net 中的 Application.Exit() 和 FormClosing 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9886073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:01:09  来源:igfitidea点击:

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

vb.netformclosing

提问by Operagust

I have a single windows form application that is running in system tray icon.If the user press X button of the windows form a messagebox is displayed with Yes and No ( Yes ->close the form---No->keep the form running in system tray icon). I was thinking to prevent the scenario when the user open another instance of the application when there is already an instance running so i have used this code :

我有一个在系统托盘图标中运行的 Windows 窗体应用程序。如果用户按下 Windows 窗体的 X 按钮,则会显示一个带有 Yes 和 No ( Yes -> close the form --- No-> keep the form running在系统托盘图标中)。我想防止当用户打开应用程序的另一个实例时已经有一个实例在运行时出现的情况,所以我使用了以下代码:

 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 the message is displayed but after i press ok, a new messagebox appears (that one from Private Sub Form_FormClosing ).If i choose NO i will have to instance running! I have read that Application.Exit fires the Form_FormClosing event.

问题是,当我想测试这个消息时会显示消息,但在我按下确定后,会出现一个新消息框(来自 Private Sub Form_FormClosing 的那个)。如果我选​​择 NO 我将不得不实例运行!我读过 Application.Exit 会触发 Form_FormClosing 事件。

Is there any possibility to cancel the triggering of the Form_FormClosing event,or am i doing something wrong?

有没有可能取消 Form_FormClosing 事件的触发,或者我做错了什么?

'this is the formclosing 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 don't be to harsh with me:)

PS:我不是程序员所以请不要对我太苛刻:)

采纳答案by Matt Wilko

You don't need to Kill the current process or use the EndStatement. If you have to use these then there is something amiss with your application.

您不需要杀死当前进程或使用该End语句。如果您必须使用这些,那么您的应用程序就会出现问题。

When you want to end your application use Me.Close. This will fire the FormClosingevent:

当您想结束您的应用程序时,请使用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

To stop more than one copy of your application from running use the option to Make Single Instance Application

要停止运行多个应用程序副本,请使用Make Single Instance Application选项

回答by Jasmeet

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 the exefrom Taskbaror kill Process. If user Close the Application from taskbar.

它将从任务栏关闭exe或杀死进程。如果用户从任务栏关闭应用程序。

CloseReason.UserClosing 

event will close the applicationif it is closed by User from Taskber

如果用户从Taskber关闭该应用程序,则该 事件将关闭该应用程序

回答by Mark Hall

In the situation where you are just starting your application and are testing for previous instances I have used the VB EndStatement to terminate the application.

在您刚刚启动应用程序并测试以前的实例的情况下,我使用了VB End语句来终止应用程序。

The End statement stops code execution abruptly, and does not invoke the Dispose or Finalize method, or any other Visual Basic code. Object references held by other programs are invalidated. If an End statement is encountered within a Try or Catch block, control does not pass to the corresponding Finally block.

End 语句会突然停止代码执行,并且不会调用 Dispose 或 Finalize 方法或任何其他 Visual Basic 代码。其他程序持有的对象引用无效。如果在 Try 或 Catch 块中遇到 End 语句,则控制不会传递到相应的 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