gpt4 book ai didi

linux - dotnet 核心 System.Threading.Timer 只触发一次

转载 作者:行者123 更新时间:2023-12-04 14:00:36 24 4
gpt4 key购买 nike

在 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();
}

最佳答案

我遇到了同样的问题,我正在使用 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
}
}

关于linux - dotnet 核心 System.Threading.Timer 只触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com