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 94021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:26:10+00:00 2026-05-10T23:26:10+00:00

I have a method which should be delayed from running for a specified amount

  • 0

I have a method which should be delayed from running for a specified amount of time.

Should I use

Thread thread = new Thread(() => {     Thread.Sleep(millisecond);     action(); }); thread.IsBackground = true; thread.Start(); 

Or

Timer timer = new Timer(o => action(), null, millisecond, -1); 

I had read some articles about how using Thread.Sleep is bad design. But I don’t really understand why.

However, for using Timer, Timer has a dispose method. Since the execution is delayed, I don’t know how to dispose the Timer. Do you have any suggestions?

Or, if you have an alternative suggestion for delaying code execution, that would also be appreciated.

  • 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. 2026-05-10T23:26:10+00:00Added an answer on May 10, 2026 at 11:26 pm

    One difference is that System.Threading.Timer dispatches the callback on a thread pool thread, rather than creating a new thread every time. If you need this to happen more than once during the life of your application, this will save the overhead of creating and destroying a bunch of threads (a process which is very resource intensive, as the article you reference points out), since it will just reuse threads in the pool, and if you will have more than one timer going at once it means you will have fewer threads running at once (also saving considerable resources).

    In other words, Timer is going to be much more efficient. It also may be more accurate, since Thread.Sleep is only guaranteed to wait at LEAST as long as the amount of time you specify (the OS may put it to sleep for much longer). Granted, Timer is still not going to be exactly accurate, but the intent is to fire the callback as close to the specified time as possible, whereas this is NOT necessarily the intent of Thread.Sleep.

    As for destroying the Timer, the callback can accept a parameter, so you may be able to pass the Timer itself as the parameter and call Dispose in the callback (though I haven’t tried this — I guess it is possible that the Timer might be locked during the callback).

    Edit: No, I guess you can’t do this, since you have to specify the callback parameter in the Timer constructor itself.

    Maybe something like this? (Again, haven’t actually tried it)

    class TimerState {     public Timer Timer; } 

    …and to start the timer:

    TimerState state = new TimerState();  lock (state) {     state.Timer = new Timer((callbackState) => {         action();         lock (callbackState) { callbackState.Timer.Dispose(); }         }, state, millisecond, -1); } 

    The locking should prevent the timer callback from trying to free the timer prior to the Timer field having been set.


    Addendum: As the commenter pointed out, if action() does something with the UI, then using a System.Windows.Forms.Timer is probably a better bet, since it will run the callback on the UI thread. However, if this is not the case, and it’s down to Thread.Sleep vs. Threading.Timer, Threading.Timer is the way to go.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure what you're asking, but part of the design… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer You are using MembershipProvider in your application, so all you… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer Not really. Your typical 1-byte compare instruction takes 1 cycle.… May 12, 2026 at 12:24 am

Related Questions

Let's say I have a class that implements the IDisposable interface. Something like this:
I have 14 LINQ queries to resolve in one method. None of them have
I have a method which should be executed in an exclusive fashion. Basically, it's
What is the fastest way to execute a method on an ASP.NET website? The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.