7

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.

js0823
  • 1,843
  • 8
  • 24
  • 36
  • 2
    So... use a shorter sleep time? 10 800 000 milliseconds is three hours. Timers and sleeping serve different purposes, you can't really replace one with another. – millimoose Feb 02 '12 at 19:11
  • 2
    Er, why did you ask for a sleep time of 10800 seconds? – Qwertie Feb 02 '12 at 19:12
  • 2
    Just to clarify, do you really want to sleep for `10800000` milliseconds (3 hours)? – keyboardP Feb 02 '12 at 19:13
  • See MSDN for an example with timers: http://msdn.microsoft.com/fr-fr/library/system.timers.timer.aspx – ken2k Feb 02 '12 at 19:13
  • 1
    If you use sleep in the gui thread...it will freeze all user input. Don't do that. – Steve Wellens Feb 02 '12 at 19:14
  • Yes I do need the thread to sleep for 3 hours, sometimes even 5 hours. – js0823 Feb 02 '12 at 19:15
  • 1
    "Freezes the software", if you sleep a different thread other than the main thread it shouldnt freeze. Sleeping for 3 hours is a long time. What exactly are you tring to do? Why do you want the thread to sleep for 3 hours? – craig1231 Feb 02 '12 at 19:15
  • If your trying to call a function every 3 hours, sleeping the thread is not the best way to use it. – craig1231 Feb 02 '12 at 19:17
  • What kind of "machines" are these? Do they have a PLC? If you are waiting for the machine to be ready for something you should probably have some sort of data link between the machine and the HMI - something to let the HMI know that it is ready. This almost sounds scary... – J... Feb 02 '12 at 19:26
  • This sounds like some kind of batch process you are running ? Why is it always 3 hours, could the period get longer ? If you are batch processing and waiting for "other machines" to complete tasks you might want to consider splitting your code into before and after chunks and using a job scheduler e.g. Windows Scheduler, AutoMate or something like that. Putting your code to sleep for 3 hours sounds possibly like a bad idea, even with a timer. – Chris Jul 11 '13 at 12:10

10 Answers10

6

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.

4

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();
laalto
  • 150,114
  • 66
  • 286
  • 303
JeremyK
  • 1,075
  • 1
  • 22
  • 45
4

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
}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
2

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.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • If the requirement **is** wait for 3 hours before running some code, then using `Thread.Sleep` isn't what he should use. – ken2k Feb 02 '12 at 19:20
  • 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). – JeremyK Feb 02 '12 at 19:25
  • 1
    @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. – M.Babcock Feb 02 '12 at 19:27
2

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)

Will Dean
  • 39,055
  • 11
  • 90
  • 118
1

You can replace

Thread.Sleep(X);

by

Task.WaitAll(Task.Delay(X));
Philippe
  • 3,945
  • 3
  • 38
  • 56
1

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

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

Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
1

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.

mbeckish
  • 10,485
  • 5
  • 30
  • 55
1

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

Arash N
  • 324
  • 1
  • 2
  • 10
0

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?

deltree
  • 3,756
  • 1
  • 30
  • 51