Alternative to Thread.Sleep in C#?

20,452

Solution 1

Your software would freeze if that sleep is on the main thread. Keep in mind the parameter is in milliseconds. 10 800 000 milliseconds = 10 800 seconds

Another way to pass the time is to pass a TimeSpan object instead. Ex:

// Sleep for 10 seconds
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 10));

As for timer:

You can use System.Timers.Timer;

Timer timer = new Timer();
timer.Interval = 20; // milliseconds
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.AutoReset = true; // if not set you will need to call start after every event fired in the elapsed callback
timer.Start();

Solution 2

USE A TIMER!

private DispatcherTimer Timer;
public Constructor
{
    Timer = new System.Windows.Threading.DispatcherTimer();
    Timer.Tick += new EventHandler(Timer_Tick);
    Timer.Interval = new TimeSpan(0,0,10);
    Timer.Start();
}


private void Timer_Tick(object sender, EventArgs e)
{
    Timer.Stop();
    Timer -= Timer_Tick;
    Timer = null;
    // DO SOMETHING
}

Solution 3

The Thread.Sleep is what you want to use for this, you may want to use a more reasonable sleep period than 3 hours though.

Update:

After reading some of the comments, Thread.Sleep is probably not what you want. Use a System.Threading.Timer instead as others have suggested.

Solution 4

Your problem is that you're blocking the main thread of your application, which is responsible for keeping the ui running. You shouldn't do this. Instead use a timer - the Forms one is probably easiest in a Forms app, or consider BackgroundWorker. (For such a long wait a timer is probably more suitable)

Solution 5

Thread.Sleep would typically be used to pause a separate thread, not in the main thread of your app.

Timer would typically be used to periodically cause the main thread to stop its normal operations and handle an event.

Either method can be used to periodically perform a function after a certain time interval.

What I wouldn't do is ask the main thread to sleep for 3 hours.

Share:
20,452
js0823
Author by

js0823

Updated on April 07, 2020

Comments

  • js0823
    js0823 about 4 years

    I have a code which when run, it executes series of lines in sequence. I would like to add a pause in between.

    Currently, I have it like this

    //do work
    Thread.Sleep(10800000);
    //do work
    

    This however freezes the software, and I think it's because sleep time is way too long. I searched online and I found another work around called Timer. Can someone show me a sample code for Timer which works just like Thread.sleep?

    Thank you in advance.

    Edit : I need the software to wait for 3 hours before proceeding with rest of the code execution. This software is meant to communicate with machines, and I cannot let the rest of the code execute while waiting for 3 hours.

  • ken2k
    ken2k about 12 years
    If the requirement is wait for 3 hours before running some code, then using Thread.Sleep isn't what he should use.
  • JeremyK
    JeremyK about 12 years
    It could be an always on problem. Nor does it says its on a form, could be a service or command prompt. While it doesnt sound like he was doing it correctly, there are tons of reasons to have things happen once ever x hours (db backup, clean up functions, etc).
  • M.Babcock
    M.Babcock about 12 years
    @JeremyK - I'll give you that I read too much into his comments, but see his update, unfortunately he's not doing any of those.