计时器方法将在哪个线程中运行? [英] Which thread will timer method run in?
本文介绍了计时器方法将在哪个线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望定期运行一个方法,但希望通过使其在单独的线程中运行来优化我的代码.到目前为止,我的代码如下所示:
I looking to run a method periodically but would like to optimise my code by having it run in a separate thread. So far my code looks something like below:
private System.Timers.Timer timerQuartSec = new System.Timers.Timer(250);
private Thread quarterSecThread;
timerQuartSec.Elapsed += new System.Timers.ElapsedEventHandler(someMethod);
quarterSecThread = new Thread(new ThreadStart(timerQuartSec.Start));
我的问题是,此代码将只是启动计时器还是在新线程上运行代码(在TimerElapsed上)?
My question is, would this code simply start the timer or would the code (on TimerElapsed) run on the new Thread?
推荐答案
System.Timers.Timer
将在ThreadPool
线程上运行,只要您不设置计时器的SynchronizingObject
.
System.Timers.Timer
will run on a ThreadPool
thread as long as you don't set the timer's SynchronizingObject
.
因此无需启动专用线程.但是,如果要访问GUI元素,则需要注意.
So there's no need to start a dedicated thread. You need to pay attention though if you want to access GUI elements.
这篇关于计时器方法将在哪个线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文