【发布时间】:2020-10-01 10:37:49
【问题描述】:
我在主程序中将计时器逻辑设置为每 30 秒一次 AutorReset,因此我的 LogCollection 方法(提取数据)将每 30 秒循环一次,并且我的代码可以继续运行。
问题是我意识到有时给定的时间比方法执行时间短,这会导致多个方法同时调用,从而导致多个数据泛滥。
问:如何让计时器在方法完成之前等待而不做另一个循环?
定时器代码如下:
Timer thetimer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds);
thetimer.Elapsed += new System.Timers.ElapsedEventHandler(LogCollector.LogCollection);
thetimer.AutoReset = true;
thetimer.Start();
我正在考虑添加一些 While 循环,但我的代码将永远循环,其余代码将无法完成。 请指教
【问题讨论】:
-
.NET 中有许多名为
Timer的类 - 您使用的是哪一个? -
using System.Timers;