tags:

views:

60554

answers:

5

How do I get my C# program to sleep for 50 milliseconds?

This might seem an easy question, but I'm having a temporary brain failure moment!

+4  A: 
Thread.Sleep
Roger Lipscombe
+53  A: 
System.Threading.Thread.Sleep(50);

Remember though, that doing this in the main GUI thread will block your GUI from updating (it will feel "sluggish")

Isak Savo
+20  A: 

Use this code

using System.Threading;
// ...
Thread.Sleep(50);
Alexander Prokofyev
+6  A: 
Thread.Sleep(50);

The thread will not be scheduled for execution by the operating system for the amount of time specified. This method changes the state of the thread to include WaitSleepJoin.

This method does not perform standard COM and SendMessage pumping. If you need to sleep on a thread that has STAThreadAttribute, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a timeout interval.

Thread.Join
SelvirK
+30  A: 

You can't specify an exact sleep time in Windows. You need a real-time OS for that. The best you can do is specify a minimum sleep time. Then it's up to the scheduler to wake up your thread after that. And never call .Sleep() on the GUI thread.

Joel Coehoorn