システムトレイアイコンで実行されている単一のWindowsフォームアプリケーションがあります。ユーザーがWindowsフォームの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
問題は、これをテストしたいときにメッセージが表示されることですが、[OK] を押すと、新しいメッセージ ボックスが表示されます (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: 私はプログラマーではないので、私に厳しくしないでください:)