.NET 定时器异步运行吗? [英] Do .NET Timers Run Asynchronously?
问题描述
我使用 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 threadSystem.Timers.Timer
executes in a thread-pool thread unless you specify aSynchronizingObject
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屋!