I use System.Threading Timers to poll sensors on different threads (one delay was slow due to latency of communication). It also allows the user to change the polling rate by changing the timer period.
What I cannot decide occurs if the timers do not end before the next period. I wrote a test program, but actually did not answer.
If I have a function that launches ~ 1.7s and calls it every 10 seconds, it ends before the next launch, and my processor usage fluctuates between 13% (one core 100%) and 0%.
t = new Timer(doWork, null, 0, 10000);
private void doWork(object o)
{
for(int i = 0; i < numberItts; i++)
{
}
}
If I then reduce the timer period to 1 s, I would expect it to not execute the thread until the previous one has been completed, or to create new threads, and CPU usage will increase as more threads begin before another termination. In fact, CPU usage ranges from 13% to 25%.
Changing the period up to 500 ms, CPU usage ranges from 38% to 50%. Of course, at this moment they should begin much faster than they end.
How are these threads managed? What limits the amount created when the polling speed is faster than the speed at which threads can be completed?
source
share