我真的为此感到挣扎.我正在Visual Studio中创建一个winforms应用程序,并且需要一个每半小时滴答一次的后台计时器-这样做的目的是从服务器中提取更新.
I'm really struggling with this. I'm creating a winforms application in visual studio and need a background timer that ticks once every half hour - the purpose of this is to pull down updates from a server.
我尝试了几种不同的方法,但是由于失败的教程/示例,或者由于我自己在C#中的缺点,它们都失败了.我认为向您展示我已经尝试过的东西会浪费时间,因为看来我尝试过的事情远远超出了预期.
I have tried a couple of different approaches but they have failed, either due to poor tutorial/examples, or to my own shortcomings in C#. I think it would be a waste of time to show you what I have tried so far as it seems what I tried was pretty far off the mark.
有没有人知道实现C#新手容易理解的异步后台计时器的简单明了的方法?
Does anyone know of a clear and simple way of implementing an asynchronous background timer that is easily understandable by a C# newbie?
推荐答案// Create a 30 min timer timer = new System.Timers.Timer(1800000); // Hook up the Elapsed event for the timer. timer.Elapsed += OnTimedEvent; timer.Enabled = true; ... private static void OnTimedEvent(object source, ElapsedEventArgs e) { // do stuff }
通常会有以下警告:计时器不会非常准确,可能需要GC.KeepAlive(timer)
with the usual caveats of: timer won't be hugely accurate and might need to GC.KeepAlive(timer)
另请参阅:为什么System.Timers.Timer在GC中幸存,而在System.Threading.Timer中却不行?
更多推荐
创建后台计时器以异步运行
发布评论