Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:43:54+00:00 2026-05-27T14:43:54+00:00

I was reading over some threading basics and on the msdn website I found

  • 0

I was reading over some threading basics and on the msdn website I found this snippet of code.

    // Put the main thread to sleep for 1 millisecond to
    // allow the worker thread to do some work:
    Thread.Sleep(1);

Here is a link to the the page: http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx.

Why does the main thread have sleep for 1 millisecond? Will the secondary thread not start its tasks if the main thread is continuously running? Or is the example meant for a task that takes 1 millisecond to do? As in if the task generally takes 5 seconds to complete the main thread should sleep for 5000 milliseconds?

If this is solely regarding CPU usage, here is a similar Question about Thread.Sleep.

Any comments would be appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:43:55+00:00Added an answer on May 27, 2026 at 2:43 pm

    The 1 in that code is not terribly special; it will always end up sleeping longer than that, as things aren’t so precise, and giving up your time slice does not equal any guarantee from the OS when you will get it back.

    The purpose of the time parameter in Thread.Sleep() is that your thread will yield for at least that amount of time, roughly.

    So that code is just explicitly giving up its time slot. Generally speaking, such a bit of code should not be needed, as the OS will manage your threads for you, preemptively interrupting them to work on other threads.

    This kind of code is often used in “threading examples”, where the writer wants to force some artificial occurrence to prove some race condition, or the like (that appears to be the case in your example)


    As noted in Jon Hanna’s answer to this same question, there is a subtle but important difference between Sleep(0) and Sleep(1) (or any other non-zero number), and as ChrisF alludes to, this can be important in some threading situations.

    Both of those involve thread priorities; Threads can be given higher/lower priorities, such that lower priority threads will never execute as long as there are higher priority threads that have any work to do. In such a case, Sleep(1) can be required… However…

    Low-priority threads are also subject to what other processes are doing on the same system; so while your process might have no higher-priority threads running, if any others do, yours still won’t run.

    This isn’t usually something you ever need to worry about, though; the default priority is the ‘normal’ priority, and under most circumstances, you should not change it. Raising or lowering it has numerous implications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing some code reading and stumbled upon this snippet that I haven't seen before:
I have been reading over some code lately and came across some lines such
I was reading over the documentation for query hints: http://msdn.microsoft.com/en-us/library/ms181714(SQL.90).aspx And noticed this: FAST
I'm just reading a bit into JavaScript and came over some code that handle
After reading over some of the other responses to this type of question I
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
I'm reading some MPEG Transport Stream protocol over UDP and it has some funky
Reading over the responses to this question Disadvantages of Test Driven Development? I got
I was reading over this question and wondered if the accepted answer might also
I was reading over an article that shows some really good information and benchmarks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.