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

  • SEARCH
  • Home
  • 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 7032275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:54:49+00:00 2026-05-28T00:54:49+00:00

I often see it mentioned that Thread.Sleep(); should not be used, but I can’t

  • 0

I often see it mentioned that Thread.Sleep(); should not be used, but I can’t understand why this is so. If Thread.Sleep(); can cause trouble, are there any alternative solutions with the same result that would be safe?

eg.

while(true)
{
    doSomework();
    i++;
    Thread.Sleep(5000);
}

another one is:

while (true)
{
    string[] images = Directory.GetFiles(@"C:\Dir", "*.png");

    foreach (string image in images)
    {
        this.Invoke(() => this.Enabled = true);
        pictureBox1.Image = new Bitmap(image);
        Thread.Sleep(1000);
    }
}
  • 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-28T00:54:49+00:00Added an answer on May 28, 2026 at 12:54 am

    The problems with calling Thread.Sleep are explained quite succinctly here:

    Thread.Sleep has its use: simulating lengthy operations while testing/debugging on an MTA thread. In .NET there’s no other reason to use it.

    Thread.Sleep(n) means block the current thread for at least the number
    of timeslices (or thread quantums) that can occur within n
    milliseconds.
    The length of a timeslice is different on different versions/types of
    Windows and different processors and generally ranges from 15 to 30
    milliseconds. This means the thread is almost guaranteed to block for
    more than n milliseconds. The likelihood that your thread will
    re-awaken exactly after n milliseconds is about as impossible as
    impossible can be. So, Thread.Sleep is pointless for timing.

    Threads are a limited resource, they take approximately 200,000 cycles
    to create and about 100,000 cycles to destroy. By default they
    reserve 1 megabyte of virtual memory for its stack and use 2,000-8,000
    cycles for each context switch. This makes any waiting thread a
    huge waste.

    The preferred solution: WaitHandles

    The most-made-mistake is using Thread.Sleep with a while-construct (demo and answer, nice blog-entry)

    EDIT:
    I would like to enhance my answer:

    We have 2 different use-cases:

    1. We are waiting because we know a
      specific timespan when we should continue (use Thread.Sleep, System.Threading.Timer or alikes)

    2. We are waiting because some condition changes some time …
      keyword(s) is/are some time! if the condition-check is in our code-domain, we
      should use WaitHandles – otherwise the external component should
      provide some kind of hooks … if it doesn’t its design is bad!

    My answer mainly covers use-case 2

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

Sidebar

Related Questions

In this stackoverflow answer a commenter mentioned that private nested classes can be quite
I often see example repository patterns that look like this: public interface IRepository<T> {
I often see this in code: var me = this; . Why is that?
I often see in PHP MVC applications an Apache RewriteRule that looks like this:
It seems to be the general opinion that tables should not be used for
I often see this doctype declaration on some pages that I am viewing <!DOCTYPE
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I often see something like that: something.property|escape something is an object, property is it's
I often see people saying that certain software is very opinionated or that Microsoft
I often see people asking XML/XSLT related questions here that root in the inability

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.