10

I want to make an application to execute several instructions to pass the following instruction has to wait a few milliseconds.

Such that:

while(true){

  send("OK");
  wait(100); //or such delay(100);

}

Is it possible in C#?

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
FredVaz
  • 423
  • 3
  • 8
  • 14
  • Possible duplicate of [C# delayed function calls](http://stackoverflow.com/questions/545533/c-sharp-delayed-function-calls) – Korayem Aug 03 '16 at 11:52

8 Answers8

23

Thread.Sleep(100); will do the trick. This can be found in the System.Threading namespace.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
11

You can use the Thread.Sleep() method to suspend the current thread for X milliseconds:

// Sleep for five seconds
System.Threading.Thread.Sleep(5000);
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
6

To sleep for 100ms use Thread.Sleep

While(true){    
  send("OK");
  Thread.Sleep(100);
}
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
5

Thread.Sleep(milliseconds) should stop the application for that second. Read up on Thread.Sleep in MSDN.

The Mask
  • 17,007
  • 37
  • 111
  • 185
iefpw
  • 6,816
  • 15
  • 55
  • 79
  • 2
    -1: The overload of `Thread.Sleep` that takes an `int` treats the number passed as *milliseconds*. (There is also an overload taking a `TimeSpan` of course.) – Richard Feb 09 '12 at 14:50
5

You can use Thread.Sleep, but I'd highly discourage it, unless you A) know for a fact it is appropriate and B) there are no better options. If you could be more specific in what you want to achieve, there will likely be better alternatives, such as using events and handlers.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
4

This would be a much better option as it doesn't lock up the current thead and make it unresponsive.

Create this method

    async System.Threading.Tasks.Task WaitMethod()
    {
        await System.Threading.Tasks.Task.Delay(100);
    }

Then call like this

    public async void MyMethod()
    {
        while(true)
        {
            send("OK");
            await WaitMethod();
        }
    }

Don't forge the async in the method that calls the delay!

For more info see this page I found when trying to do a similar thing

Notts90
  • 254
  • 2
  • 20
4

Yes, you can use the Thread.Sleep method:

while(true)
{
  send("OK");
  Thread.Sleep(100); //Sleep for 100 milliseconds
}

However, sleeping is generally indicative of a suboptimal design decision. If it can be avoided I'd recommend doing so (using a callback, subscriber pattern etc).

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
1

Thread.Sleep() is the solution.It can be used to wait one thread untill other thread completes it's working OR After execution of one instruction someone wants to wait time before executing the later statement. It has two overloaded method.first take int type milisecond parameter while second take timespan parameter. 1 Milisecond=1/1000s OR 1 second=1000 Miliseconds Suppose if someone wants to wait for 10 seconds code will be....

System.Threading.Thread.Sleep(10000);

for more details about Thread.Sleep() please visit http://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx

Happy Coding.....!

Abdul
  • 854
  • 1
  • 9
  • 11