我有一个在系统托盘图标中运行的窗体应用程序。如果用户按下窗体的 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:我不是程序员,所以请不要对我苛刻:)