Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果已定义并正在运行设计器创建的计时器,则不会触发Form FormClosing事件_C#_.net_Windows_Timer_Formclosing - Fatal编程技术网

C# 如果已定义并正在运行设计器创建的计时器,则不会触发Form FormClosing事件

C# 如果已定义并正在运行设计器创建的计时器,则不会触发Form FormClosing事件,c#,.net,windows,timer,formclosing,C#,.net,Windows,Timer,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; .... th

如果我尝试右键单击关闭最小化表单应用程序或使用任务管理器应用程序选项卡结束流程,则在定义并运行/启用计时器时不会触发表单关闭事件

//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);
    }
//
///清理所有正在使用的资源。
/// 
///如果应处置托管资源,则为true;否则,错误。
受保护的覆盖无效处置(布尔处置)
{
MessageBox.Show(“Disposing”,“Disposing”,MessageBoxButtons.OK);
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
当计时器1被禁用时,此Dispose方法与Form_FormClosing事件一样被调用,但当计时器1被启用时,这两种方法似乎都没有被调用,在这种情况下,end task是终止应用程序/表单的唯一方法。可能是线程太忙,无法处理关闭请求吗?如果是这样,如何使线程周期性地中断以允许它服务于关闭请求


问题是,该应用程序无法有序关闭,例如,允许提示用户保存数据,或者他们真的打算关闭该应用程序。问题在于,当某些数据库不可用时,计时器陷入循环,因此单线程无法处理其他消息/事件。一旦计时器退出循环,通过超时或到达所需的数据库,所有排队的消息/事件都会得到正确处理。感谢所有受访者的时间和反馈,这有助于思维过程,并允许您为某些行为寻找其他原因。:)

问题在于,当某些数据库不可用时,计时器陷入循环中,因此单线程无法处理其他消息/事件。一旦计时器退出循环,通过超时或到达所需的数据库,所有排队的消息/事件都会得到正确处理。感谢所有受访者的时间和反馈,这有助于思维过程,并允许您为某些行为寻找其他原因。:)

当整个应用程序被拆除时,需要禁用计时器吗。这并不是说一旦整个过程停止存在,它就会继续运行。