我有两个计时器, aSystem.Timers.Timer和 a System.Windows.Forms.Timer。现在每 1.5 秒都在调用一个方法。但是它们不是同步的,因为有时调用的方法System.Timers.Timer需要更长的时间才能完成。有没有办法让调用System.Windows.Forms.Timer的方法等待直到方法System.Timers.Timer完成?到目前为止我尝试过的: private void FileReadFunction(object sender, EventArgs e) { try { foreach (NetPinger.source.AddGraph b in graphList) { b.fileRead(); } } catch(Exception ex) { MessageBox.Show(Convert.ToString(ex)); } myTimer.Stop(); aTimer.Start(); } public void prepData(object objectInfo, EventArgs e) { foreach (NetPinger.source.AddGraph b in graphList) { b.prepareData(); } aTimer.Stop(); myTimer.Start(); }
2 回答
![?](http://img1.sycdn.imooc.com/54584c9c0001489602200220-100-100.jpg)
慕的地8271018
TA贡献1796条经验 获得超4个赞
System.Timers.Timer
每 1.5 秒仅使用和 准备数据,然后Invoke
在同一事件处理程序中立即更新 UI using方法。
![?](http://img1.sycdn.imooc.com/54586425000103f602200220-100-100.jpg)
收到一只叮咚
TA贡献1821条经验 获得超4个赞
干得好:
Timer timer1;
Timer timer2;
private void Timer1_Elapsed()
{
//just to make sure your timer1 & timer2 doesn't fire before you do your work
timer1.Stop();
timer2.Stop();
// Do What you want here
timer2.Start();
}
private void Timer2_Elapsed()
{
timer2.Stop();
//Do what you want here
timer1.Start();
}
只需确保最初启动您的 timer1。
- 2 回答
- 0 关注
- 198 浏览
添加回答
举报
0/150
提交
取消