【问题标题】:Application.Exit() and FormClosing event in Vb.netVb.net 中的 Application.Exit() 和 FormClosing 事件
【发布时间】:2012-04-10 18:43:00
【问题描述】:

我有一个在系统托盘图标中运行的窗体应用程序。如果用户按下窗体的 X 按钮,则会显示一个消息框,其中包含是和否(是 -> 关闭窗体---否 -> 保留在系统托盘图标中运行的表单)。 我正在考虑防止当用户打开另一个应用程序实例时已经有一个实例正在运行,所以我使用了这段代码:

 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

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

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

'这是合页程序

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:我不是程序员,所以请不要对我苛刻:)

【问题讨论】:

    标签: vb.net formclosing


    【解决方案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
    

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

    CloseReason.UserClosing 
    

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

    【讨论】:

      【解决方案2】:

      您不需要终止当前进程或使用End 语句。如果您必须使用这些,那么您的应用程序就有问题。

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

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

      【讨论】:

      • 感谢您的回答...我从 Visual Studio Properties 中找到了设置,使用此方法也应该没问题...不幸的是我没有解决为什么我的应用程序没有关闭关闭后从任务管理器...这就是为什么我找到这个临时解决方案....基本上该应用程序是一个简单的 Windows 窗体。
      【解决方案3】:

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

      End 语句突然停止代码执行,并且不调用 Dispose 或 Finalize 方法,或任何其他 Visual Basic 代码。目的 其他程序持有的引用无效。如果结束语句 在 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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多