2012-03-27 13 views
5

Sistem tepsisi simgesinde çalışan tek bir windows form uygulamasına sahibim. Kullanıcı pencerelerin X düğmesine basarsa, Evet ve Hayır ile bir mesaj kutusu görüntülenir (Evet) -> formu kapatın --- Hayır-> sistem tepsisi simgesinde çalışan formu saklayın). Ben kullanıcı uygulamasını başka bir örneğini açtığınızda ben bu kodu kullanmış böylece çalışan bir örneği zaten varken senaryoyu engellemek için düşünüyordum:Vb.net'de Application.Exit() ve FormClosing olayı

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 

sorun olduğunu ben bu mesajı test etmek istediğinizde görüntülendikten sonra, Tamam'a bastıktan sonra yeni bir mesaj kutusu (Özel Sub Form_FormClosing'den) görünür. NO'yu seçtiğimde, örneğin çalışmam gerekecek! Application.Exit'in Form_FormClosing olayını tetiklediğini okudum.

Form_FormClosing olayının tetiklenmesini iptal etme olasılığı var mı, yoksa yanlış bir şey mi yapıyorum?

'Bu formclosing prosedürü

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: Ben bir programcı böylece benimle sert etmek olmayın lütfen :) durumda

cevap

5

güncel işlem öldür ya End Bildirimi kullanmaya gerek yoktur. Bunları kullanmanız gerekiyorsa, uygulamanızla ilgili bir sorun var demektir.

Uygulamanızı sonlandırmak istediğinizde Me.Close kullanın. Bu vazife den exe ​​ kapatın veya öldürecek Make Single Instance Application

+0

Cevabınız için teşekkürler ... Visual Studio Properties öğesinden ayarları buldum ve bu yöntemi kullanmak için de tamam olmalı ...maalesef benim aplikasyon kapattıktan sonra görev yöneticim kapatmıyor neden yönetmedim ... bu geçici çözüm bulmak neden bu .... Temelde uygulama basit bir windows formudur. – Operagust

1

sadece başvurunuzu başlıyor nerede olduğumu ve önceki örnekleri test ediyorsanız uygulamayı sonlandırmak için VB End Bildirimi kullandım.

sonu deyimi

aniden kod yürütülmesine durur ve bertaraf çağırmak veya yöntemi veya başka herhangi bir Visual Basic kodu sonlandırın etmez. Diğer programlar tarafından tutulan nesne başvuruları geçersiz kılındı. Bir Try veya Catch bloğu içinde bir End ifadesi ile karşılaşılırsa, kontrol ilgili Sonuncu bloğa geçmez.

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

Büyük dan Kullanıcı tarafından kapalıysa, uygulamayı kapatmak çok teşekkür ederim edecek, bunu değişti ve işe yaradı. – 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 

için

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 

kullanacağım seçeneğini yayınını uygulamanın birden fazla kopyasını durdurmak için: Bu FormClosing olay ateş edeceği Süreç. Kullanıcı görev çubuğundan Uygulamasını kapatın.

CloseReason.UserClosing 

olay o Taskber

İlgili konular