.NET 定时器异步运行吗? [英] Do .NET Timers Run Asynchronously?

查看:18
本文介绍了.NET 定时器异步运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jabber-net(<一个 href="http://en.wikipedia.org/wiki/List_of_XMPP_library_software" rel="noreferrer">XMPP 库.)

如果由于某种原因与服务器的连接终止,我想做的是每隔一分钟左右尝试连接一次.

What I would like to do, if for some reason the connection to the Server is ended, is keep trying to connect every minute or so.

如果我启动一个计时器以在下一次尝试之前等待一段时间,该计时器是否异步运行并且产生的 Tick 事件加入主线程,或者我是否需要启动我自己的线程并从内部启动计时器在那里?

If I start a Timer to wait for a period of time before the next attempt, does that timer run asynchronously and the resulting Tick event join the main thread, or would I need to start my own thread and start the timer from within there?

推荐答案

你使用的是哪种定时器?

What kind of timer are you using?

  • System.Windows.Forms.Timer 将在 UI 线程中执行
  • System.Timers.Timer 在线程池线程中执行,除非您指定 SynchronizingObject
  • System.Threading.Timer 在线程池线程中执行其回调
  • System.Windows.Forms.Timer will execute in the UI thread
  • System.Timers.Timer executes in a thread-pool thread unless you specify a SynchronizingObject
  • System.Threading.Timer executes its callback in a thread-pool thread

在所有情况下,定时器本身都是异步的——它不会占用"一个线程,直到它被触发.

In all cases, the timer itself will be asynchronous - it won't "take up" a thread until it fires.

这篇关于.NET 定时器异步运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆