Thread.Sleep method is in the Thread class and is a static method that receives a parameter. You must include the using directive for Process.Threading or use the full name of the method invocation, which is Process.Threading.Thread.Sleep. No need to create a new topic () to use the Sleep method is static since. This example shows the runtime behavior of the Thread.Sleep method. NET Framework.
Program that sleeps (C#)
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
//
// Demonstrates three different ways of calling Sleep.
//
var stopwatch = Stopwatch.StartNew();
Thread.Sleep(0);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine(DateTime.Now.ToLongTimeString());
stopwatch = Stopwatch.StartNew();
Thread.Sleep(5000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine(DateTime.Now.ToLongTimeString());
stopwatch = Stopwatch.StartNew();
System.Threading.Thread.Sleep(1000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
//
// Bonus: shows SpinWait method.
//
stopwatch = Stopwatch.StartNew();
Thread.SpinWait(100000 * 10000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
}
Output
0 ElapsedMilliseconds after Sleep(0)
8:14:43 AM Time after Sleep(0)
4999 ElapsedMilliseconds after Sleep(5000)
8:14:48 AM Time after Sleep(5000)
999 ElapsedMilliseconds after Sleep(1000)
3144 ElapsedMilliseconds after SpinWait(Int32)
0 comments:
Post a Comment