Alternative to Thread.Sleep in C#?

2019-01-27 23:59发布

问题:

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.

回答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();


回答2:

Please see this MSDN reference on the System.Threading.Timer class. There is a great explanation, all of the class members, and some sample code for you to follow when testing/learning.

Mind you, though, the Timer should be used when you want to fire an event at a certain interval. If you are just looking to pause execution of your application, then you should go with Thread.Sleep(). However, as many others have pointed out, you are causing your thread to sleep for an extended amount of time.



回答3:

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
}


回答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)



回答5:

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.



回答6:

Have a look Thread.Sleep(300) not working correctly

Probably you need to use the "Dispatcher". Have a look here as well



回答7:

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.



回答8:

I think you should use a Monitor. It helps you to put a wait on objects and release the lock when you need to continue running the program.

You should find your answer here: Safe Thread Synchronization



回答9:

You can replace

Thread.Sleep(X);

by

Task.WaitAll(Task.Delay(X));


回答10:

You're sleeping the thread for 10800 seconds, or 3 hours. Thread.Sleep() is designed to freeze your thread, stop anything from working in the software for that duration. In this case, the duration is 18 minutes. What are you trying to do?