Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How does Sleep sleep?

VanilleBertVanilleBert Posts: 29Member
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

  • weiccoweicco Posts: 950Member
    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.
  • VanilleBertVanilleBert Posts: 29Member
    : 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.