2012-03-27 11 views
5

私は、システムトレイアイコンで実行されている単一のWindowsフォームアプリケーションを持っています。ユーザーがXボタンを押すと、メッセージボックスに「はい」と「いいえ」が表示されます - >フォームを閉じる - いいえ - >システムトレイアイコンでフォームを実行し続ける)。 私は、ユーザーがアプリケーションの別のインスタンスを開いたときに、私はこのコードを使用しているので、実行中のインスタンスが既に存在する場合のシナリオを防ぐために考えていた:Application.Exit()とVb.netのFormClosingイベント

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を押した後、新しいメッセージボックスが表示されます(プライベートSub Form_FormClosingからのもの)。私がNOを選択した場合、インスタンスを実行する必要があります! 私はそれを読んで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 formclosing手順です:私はプログラマーないですので、私と一緒に過酷にならないでください:)

答えて

5

あなたは、現在のプロセスを強制終了したりEndステートメントを使用する必要はありません。これらを使用する必要がある場合は、アプリケーションに不具合があります。

アプリケーションを終了する場合は、Me.Closeを使用してください。それは、exeファイルからタスクバーを閉じるか、殺すMake Single Instance Application

+0

答えをありがとう...私はビジュアルスタジオのプロパティから設定が見つかりましたこれはまた、このメソッドを使用するようにしてください。unfortunatelly私はなぜ私のアプリケーションが終了後にタスクマネージャからシャットダウンしていない管理していない...それは私がこの一時的な解決策を見つける理由....それは基本的にアプリケーションは単純なWindowsフォームです。 – Operagust

1

あなたは自分のアプリケーションを起動している状況で以前のインスタンスをテストしていますが、VB End Statementを使用してアプリケーションを終了しました。

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 
+0

Taskber からアプリケーションを閉じます偉大な、私はそれを変更したし、それが働いていたので、ありがとうございました。 – Operagust

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 

にオプションを使用して実行しているから、アプリケーションの複数のコピーを停止するには

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 

:これはFormClosingイベントを発生しますプロセス。ユーザーが アプリケーションを閉じる場合、タスクバーからアプリケーションを閉じます。

CloseReason.UserClosing 

それはユーザーによって閉じられている場合、イベントは

関連する問題