Geo P
Geo P

Reputation: 740

Is there a way to pause all Threading.Timer timers?

I am new to C# and I am working on a program that require several timers.

I was wondering if there was a way to pause all the timers simultaneously in a program when that program is running CPU intensive code?

At the moment, timers that are currently enabled tries to catch up with all the timer events that were raised during the intensive operation.

Upvotes: 2

Views: 1306

Answers (3)

Micah Armantrout
Micah Armantrout

Reputation: 6971

You will need to keep track of all the timers you create in a list and call the change method when you do not want them to run.

  List<System.Threading.Timer> TimerList = new List<System.Threading.Timer>();

  Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
  Timer responseTimer = new Timer(tcb, autoEvent, 1000, 250);  

  TimerList.add(stateTimer);
  TimerList.add(responseTimer);

foreach (System.Threading.Timer t in TimerList)
{
  T.Change(TimeSpan(0), new(TimeSpan(0));
}

Upvotes: 1

Carsten Sch&#252;tte
Carsten Sch&#252;tte

Reputation: 4558

There is no way in the .NET framework to get a list of all running Threading.Timer instances. But you can create your own Timer wrapper that adds all running timers to a list and of course removes them when the timer finishes.

If you only want to make sure that your methods only execute when the CPU is idleing, you can set the timer's thread priority to a lower value.

Upvotes: 0

platon
platon

Reputation: 5340

I think that you can create the list of these timers (when you are activating them) and disable these timers manually (using the Timer.Change method).

Upvotes: 2

Related Questions