创建后台计时器以异步运行

编程入门 行业动态 更新时间:2024-10-28 08:27:52
本文介绍了创建后台计时器以异步运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的为此感到挣扎.我正在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中却不行?

更多推荐

创建后台计时器以异步运行

本文发布于:2023-10-09 10:17:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   后台

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!