GroomedGorilla
GroomedGorilla

Reputation: 1002

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));

My question is, would this code simply start the timer or would the code (on TimerElapsed) run on the new Thread?

Upvotes: 1

Views: 87

Answers (1)

JeffRSon
JeffRSon

Reputation: 11186

System.Timers.Timer will run on a ThreadPool thread as long as you don't set the timer's SynchronizingObject.

So there's no need to start a dedicated thread. You need to pay attention though if you want to access GUI elements.

Upvotes: 2

Related Questions