
我尝试过以下方法:
int originalinterval = t.Interval;t.Interval = 0;t.Interval = originalinterval;
但它并不一致.
我创建了一个新的计时器,继承自System.Timers.Timer,并公开了一个“Tick”方法 – 但问题是“Elapsed”事件然后同步触发.
当我使用新线程实现“Tick”时 – 结果再次不一致.
有没有更好的方法来实现它?
解决方法 我曾经遇到过同样的问题,所以我使用autoresetEvent知道是否成功调用了Elapsed:/// <summary>/// Tickable timer,allows you to manually raise a 'Tick' (asynchronously,of course)/// </summary>public class TickableTimer : System.Timers.Timer{ public new event ElapsedEventHandler Elapsed; private System.Threading.autoresetEvent m_autoresetEvent = new System.Threading.autoresetEvent(true); public TickableTimer() : this(100) { } public TickableTimer(double interval) : base(interval) { base.Elapsed += new ElapsedEventHandler(TickableTimer_Elapsed); } public voID Tick() { new System.Threading.Thread(delegate(object sender) { Dictionary<string,object> args = new Dictionary<string,object> { {"signalTime",DateTime.Now},}; TickableTimer_Elapsed(this,Mock.Create<ElapsedEventArgs>(args)); }).Start(); this.m_autoresetEvent.WaitOne(); } voID TickableTimer_Elapsed(object sender,ElapsedEventArgs e) { m_autoresetEvent.Set(); if (this.Elapsed != null) this.Elapsed(sender,e); }} 总结 以上是内存溢出为你收集整理的c# – 告诉计时器对象以异步方式调用其“Elapsed”事件全部内容,希望文章能够帮你解决c# – 告诉计时器对象以异步方式调用其“Elapsed”事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)