使用Linux服务,当我设置System.Threading.Timer
时,它只触发一次。有趣的是它只在Linux上发射一次。在Windows上进行调试时,它会不断触发。目前我已经使用System.Timers
取而代之了。只是好奇,如果其他人看到了这一点。
private void StartTimers()
{
_autoEvent = new AutoResetEvent(false);
_tm = new Timer(_client.ProcessQueue, _autoEvent, 1000, 10000);
Console.WriteLine("Press Enter To Exit Application");
Console.ReadLine();
}
答案 0 :(得分:0)
我遇到了同样的问题,我正在使用 Debian 开发在 rasperry pi 上运行的 dotnet 核心应用程序。它应该每分钟截屏一次,但 System.Threading.Timer 只在第一次触发。按照您的建议,我尝试了 System.Timers 类,发现它有效。计时器现在按预期每分钟触发一次。所以我可以确认这个解决方案有效。
public class Client
{
public Client()
{
// start the timer in the class constructor
StartScreenshotting();
}
private void StartScreenshotting()
{
System.Timers.Timer timer = new System.Timers.Timer();
// 60000ms is one minute
timer.Interval = 60000;
timer.Elapsed += TakeScreenshot;
timer.Start();
}
private void TakeScreenshot(object sender, System.Timers.ElapsedEventArgs e)
{
// take screenshot logic
}
}