c# timer 销毁_如果表单应用程序关闭C#,如何防止发生Timer Elapsed事件

我在

Windows窗体应用程序(Visual C#)中有一个计时器,当我想退出应用程序时,它会导致问题.

计时器被定义为表单类的成员:

partial class Form1

{

//These are the members in question:

internal ComACRServerLib.Channel channel;

private System.Timers.Timer updateStuff;

}

在表单应用程序的构造函数中声明/构造计时器:

public Form1()

{

InitializeComponent();

updateStuff = new System.Timers.Timer();

updateStuff.Elapsed += new System.Timers.ElapsedEventHandler(updateStuff_Elapsed);

}

只需按一下按钮即可启动和配置计时器:

private void btnAcquire_Click(object sender, EventArgs e)

{

updateStuff.Interval = 100;

updateStuff.Enabled = true;

updateStuff.AutoReset = true;

updateStuff.Start();

}

当计时器过去时,它调用updateStuff_Elapsed,它获取要用setText显示的信息(有一些代码可以确保调用setText是线程安全的).

private void updateStuff_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

if (!channel.isOffline)

{

object[] status = channel.GetACRCustom("P6144");

setText(System.Convert.ToString(status[0]));

}

}

public delegate void setTextDelegate(string text);

public void setText(string text)

{

if (this.lblTest.InvokeRequired == true)

{

setTextDelegate d = new setTextDelegate(setText);

this.Invoke(d, new object[] { text });

}

else

{

lblTest.Text = text;

}

}

在应用程序退出时,我尝试摆脱计时器,并使用以下内容阻止它再次触发:

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

updateStuff.AutoReset = false;

updateStuff.Stop();

updateStuff.Close();

updateStuff.Dispose();

components.Dispose();

}

base.Dispose(disposing);

}

但是如果计时器是自动运行的并且我退出程序,我总是得到错误,Timer Elapsed事件调用的例程updateStuff_elapsedis试图使用已经处理过的资源!即使我已经尽力停止并在处理之前销毁计时器.

当应用程序关闭时,如何停止计时器的触发?

编辑

我尝试移动Dispose代码以试图强制关闭计时器,但没有运气.我还尝试使用updateStuff.Elapsed – = updateStuff_Elapsed在停止和处理之前删除事件调用;

protected override void Dispose(bool disposing)

{

//now this code HAS to run always.

updateStuff.Elapsed -= updateStuff_Elapsed;

updateStuff.AutoReset = false;

updateStuff.Stop();

updateStuff.Close();

updateStuff.Dispose();

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

最佳答案 如

System.Timers.Timer的文档中所述,Timer的事件处理程序调用在ThreadPool线程上排队.因此,您必须假设事件处理程序可以一次调用多次,或者可以在禁用Timer后调用.因此,事件处理程序必须设计为正确处理这些情况.

首先,将timer的SynchronizingObject属性设置为Form的实例.这将把所有事件处理程序的调用编组到UI线程,因此我们不需要打扰表单字段的锁定(我们将始终从同一个UI线程访问所有内容).使用此属性集,您也不需要在setText方法中调用this.Invoke(…).

public Form1()

{

updateStuff = new System.Timers.Timer();

updateStuff.SynchronizingObject = this;

...

}

public void setText(string text)

{

lblTest.Text = text;

}

然后创建标志,让你知道,计时器是否被处置.然后只需在事件处理程序中检查此标志:

partial class Form1

{

private bool Disposed;

....

}

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

updateStuff.Dispose();

Disposed = true;

}

base.Dispose(disposing);

}

private void updateStuff_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

if(!Disposed)

{

if (!channel.isOffline)

{

object[] status = channel.GetACRCustom("P6144");

setText(System.Convert.ToString(status[0]));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值