0

I have an Windows Form application that call a method after a period of times, repeatedly. so I'm concern of CPU usage. I know two ways to do that:

1) Using Timer component
2) callingThread.Sleep(time) in a loop

So, which one is better? Which on uses CPU less or didn't uses CPU ?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Hamed
  • 2,084
  • 6
  • 22
  • 42

2 Answers2

1

If you want to execute certain block of code periodically every certain time then use timers it is designed for that. A little bit of CPU architecture knowledge shows to you that there are general purpose timers that does not consume any processor or OS resource to generate accurate time delays.

On the other hand maintaing a thread in the suspended state and switch it to execution and enqueue it in the ready queue will consume CPU and OS resources.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
1

It doesn't matter because even if you set the loop to 1ms you couldn't really measure the CPU usage. It would be that low.

For reasons of reliability, maintainability and readability you should use a System.Windows.Forms.Timer.

usr
  • 168,620
  • 35
  • 240
  • 369