2012-03-27 2 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 

문제는 그 나는이 메시지를 테스트 할 때 표시되지만 확인을 누르면 새 메시지 상자가 나타납니다 (Private Sub Form_FormClosing의 해당 메시지 상자). 아니오를 선택하면 인스턴스를 실행해야합니다! Application.Exit이 Form_FormClosing 이벤트를 발생시키는 것을 읽었습니다.

Form_FormClosing 이벤트의 트리거링을 취소 할 가능성이 있습니까? 아니면 잘못된 것이 있습니까?

'이것은 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입니다 : 내가 아닌 프로그래머가 그렇게 나와 함께 가혹한로하지 마십시오 :) 상황에서

답변

5

당신은 현재의 프로세스를 종료하거나 End 정책을 사용할 필요가 없습니다. 이들을 사용해야 할 경우, 신청서에 문제가있을 수 있습니다.

응용 프로그램을 끝내려는 경우 Me.Close을 사용하십시오. 그것은 작업 표시 줄에서 EXE을 닫거나 죽일 것이다 Make Single Instance Application

+0

답변을 주셔서 감사합니다 ... 나는이 방법을 사용하여 확인해야합니다 비주얼 스튜디오 속성에서 설정을 발견했습니다 ...unfortunatelly 나는 왜 내 응용 프로그램이 종료 후 작업 관리자에서 종료되지 않는지 관리하지 못했습니다 ... 그것이 왜이 임시 솔루션을 찾았는지 .... 기본적으로 응용 프로그램은 간단한 Windows 양식입니다. – Operagust

1

당신이 당신의 응용 프로그램을 시작하는 곳입니다 이전 인스턴스에 대해 테스트 중이므로 VB End 문을 사용하여 응용 프로그램을 종료했습니다.

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

대에서 사용자에 의해 폐쇄되는 경우 응용 프로그램을 닫습니다 당신이 너무 감사합니다, 나는 그것을 변경하고 일했다. – 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 

이벤트가 Taskber

관련 문제