2014-01-08 3 views
0

최소화 된 폼 응용 프로그램을 닫으려면 마우스 오른쪽 단추를 클릭하거나 타이머를 처리하는 동안 FormClosing 이벤트가 트리거되지 않는 작업 관리자 응용 프로그램 탭을 사용하여 프로세스를 종료하십시오 정의되어 있고 실행 중/사용 가능합니다. 시도가/종료 아직 응용 프로그램을 종료 될 때까지 내가 타이머를 정지/해제하지 않으Form 디자이너가 만든 타이머가 정의되어 실행중인 경우 FormClosing 이벤트가 발생하지 않음

//Extract from Form designer.cs: 
.... 
private System.Windows.Forms.Timer timer1; 
.... 
this.timer1 = new System.Windows.Forms.Timer(this.components); 
this.timer1.Interval = 5000; 
.... 
this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
...... 
this.Load += new System.EventHandler(this.FormSI_Load); 
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormSI_FormClosing); 


//Extract from form.cs: 
namespace namespace 
{ 
    public partial class FormSI : Form 
    { 
     ..... 
     public FormSaturnInterface() 
     { 
      InitializeComponent(); 
      ...... 
      timer1.Interval = intTimerIntervalms; 
      //timer1.Enabled = true; //FormClosing event not triggered if timer1 enabled 
      timer1.Enabled = false; //FormClosing event triggered if timer1 not enabled 
      ........ 
      private void FormSI_Load(object sender, EventArgs e) 
      { 
        MessageBox.Show("Load Event triggered["+ e.ToString() + "]", "Load Event triggered", MessageBoxButtons.OK); //Load event always triggered with or without timer1 enabled 
      } 

      private void FormSI_FormClosing(Object sender, FormClosingEventArgs e) 
      { 
       //e.Cancel = true; //stops app/form closing 
       timer1.Stop(); 
       System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder(); 
       messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason); 
       messageBoxCS.AppendLine(); 
       messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel); 
       messageBoxCS.AppendLine(); 
       MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event"); 
      } 
      ........ 

여전히 Formclosing 이벤트가 트리거합니다. 이 시나리오에서 가능한 해결책은 무엇입니까? 어떤 도움이라도 대단히 감사하겠습니다.

FormSI.designer.cs 이미이 코드가 포함되어 있습니다주세요 :

/// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     MessageBox.Show("Disposing", "Disposing", MessageBoxButtons.OK); 

     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

타이머 1이 비활성화되었을 때 Form_FormClosing 이벤트이기 때문에이 폐기 방법이라고,하지만 둘 때 타이머 1 호출되는에 표시 이 경우 최종 작업은 앱/양식을 종료하는 유일한 방법입니다. 스레드가 닫기 요청을 처리하는 데 너무 바빠서 할 수 있습니까? 그렇다면 어떻게하면 스레드가 주기적으로 인터럽트되어 닫는 요청을 처리 할 수 ​​있습니까?

문제는 사용자가 예를 들어 데이터를 저장하라는 메시지를 표시하거나 앱 닫기를 의미하는 규칙적인 방식으로 앱을 종료 할 수 없다는 것입니다. 문제는 특정 데이터베이스를 사용할 수 없을 때 루프에서 타이머가 잡히는 것과 관련되어서 단일 스레드가 다른 메시지/이벤트를 처리 할 수 ​​없었습니다. 타이머가 루프에서 빠져 나가자 마자 시간 초과되거나 필요한 데이터베이스에 도달하면 대기중인 모든 메시지/이벤트가 올바르게 처리됩니다. 모든 응답자 들께 시간과 피드백에 감사 드리며 이는 사고 과정을 돕고 특정 행동에 대한 다른 이유를 제시합니다. :)

+0

전체 응용 프로그램을 해체 할 때 타이머를 비활성화해야하는 이유는 무엇입니까? 전체 프로세스가 중단되면 계속 실행되는 것처럼 보이지 않습니다. – Servy

답변

0

문제는 특정 데이터베이스를 사용할 수 없을 때 타이머가 루프에서 발견되어 단일 스레드에서 다른 메시지/이벤트를 처리 할 수 ​​없다는 것입니다. 타이머가 루프에서 빠져 나가자 마자 시간 초과되거나 필요한 데이터베이스에 도달하면 대기중인 모든 메시지/이벤트가 올바르게 처리됩니다. 모든 응답자 들께 시간과 피드백에 감사 드리며 이는 사고 과정을 돕고 특정 행동에 대한 다른 이유를 제시합니다. :)