سلام
به کمک این دستور میتونیم زمان اجرای برنامه چند ثانیه برنامه را نکه داریم دوباره ادامه بدیم
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)
منبع