How does Sleep sleep?

Hi,

first look what I mean with Sleep and MySleep.

Sleep:
[code]
System.Threading.Thread.Sleep(5000);
[/code]

MySleep:
[code]
DateTime DT = DateTime.Now;
while (((TimeSpan)(DateTime.Now - DT)).TotalMilliseconds < 5000) ;
[/code]

If you use Sleep, you will see no CPU-Usage-Maximum in the TaskManager.
But if you use MySleep, the CPU will reach a usage max. I understand why MySleep use the CPU, but why does Sleep don't?

How does Sleep sleep?

Comments

  • I think Sleep calls for Win32 API function Sleep which then tells Operating System "this thread is sleeping now for 5000 ms." After this process scheduler (this gets really deep into OS) schedules sleeping thread away and sets another thread into run. When scheduler has gone through every thread in the system it sees our sleeping thread. It checks if its still sleaping and if not, it starts scheduling some processor time for it again.
  • : I think Sleep calls for Win32 API function Sleep which then tells Operating System "this thread is sleeping now for 5000 ms." After this process scheduler (this gets really deep into OS) schedules sleeping thread away and sets another thread into run. When scheduler has gone through every thread in the system it sees our sleeping thread. It checks if its still sleaping and if not, it starts scheduling some processor time for it again.
    :

    Thx for this. My thirst for knowledge is satisfied.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories