5

システムトレイアイコンで実行されている単一の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: 私はプログラマーではないので、私に厳しくしないでください:)

4

3 に答える 3

5

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

アプリケーションの複数のコピーが実行されないようにするには、[単一インスタンス アプリケーションを作成] オプションを使用します。

于 2012-03-27T10:30:57.400 に答える
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からユーザーによって閉じられた場合 、イベントはアプリケーションを閉じます

于 2014-04-15T06:56:34.357 に答える
1

アプリケーションを起動したばかりで、以前のインスタンスをテストしている状況では、VB Endステートメントを使用してアプリケーションを終了しました。

End ステートメントは、コードの実行を突然停止し、Dispose メソッドや Finalize メソッド、またはその他の Visual Basic コードを呼び出しません。他のプログラムが保持するオブジェクト参照は無効になります。Try または Catch ブロック内で End ステートメントが検出された場合、制御は対応する Final ブロッ​​クに渡されません。

If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then  
   MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,         MessageBoxIcon.Exclamation) 
   End
End If 
于 2012-03-27T09:00:01.377 に答える