我该如何让我的C#程序休眠(暂停执行)50毫秒?
System.Threading.Thread.Sleep(50);
请记住,如果在主GUI线程中执行此操作,将阻止GUI更新(它会感觉“迟钝”)。
只需删除 ;
即可使其适用于VB.net。
不同的编程语言中,等待基本上有三种选择:
C#中的松散等待:
Thread.Sleep(numberOfMilliseconds);
然而,Windows线程调度程序会导致Sleep()
的准确性约为15毫秒(因此,即使计划等待1毫秒,Sleep也可以轻松地等待20毫秒)。
2. - 在C#中进行紧密等待的方法是:
Stopwatch stopwatch = Stopwatch.StartNew();
while (true)
{
//some other processing to do possible
if (stopwatch.ElapsedMilliseconds >= millisecondsToWait)
{
break;
}
}
我们也可以使用DateTime.Now
或其他时间测量的方法,但Stopwatch
更快(如果在紧密循环中,这将显得非常明显)。
对于3. - 组合:
Stopwatch stopwatch = Stopwatch.StartNew();
while (true)
{
//some other processing to do STILL POSSIBLE
if (stopwatch.ElapsedMilliseconds >= millisecondsToWait)
{
break;
}
Thread.Sleep(1); //so processor can rest for a while
}
这段代码定期阻塞线程1毫秒(或更长,具体取决于操作系统的线程调度),因此处理器在阻塞期间不会繁忙,并且代码不会消耗处理器的所有能力。在阻塞之间仍然可以执行其他处理(例如:更新UI、处理事件或执行交互/通信任务)。
在Windows中,您无法指定精确的睡眠时间。要实现这一点,您需要一个实时操作系统。您能做的最好的事情是指定最小睡眠时间。然后由调度程序在此之后唤醒您的线程。并且绝对不要在GUI线程上调用.Sleep()
方法。
现在有了 async/await 特性,最好的让程序等待50毫秒的方法是使用 Task.Delay:
async void foo()
{
// something
await Task.Delay(50);
}
如果你的目标是.NET 4(使用Async CTP 3 for VS2010或Microsoft.Bcl.Async),那么你必须使用:
async void foo()
{
// something
await TaskEx.Delay(50);
}
这样做可以避免阻塞用户界面线程。
FlushAsync
版本。 - Toni Petrinaasync
声明的替代方法是调用Task.Delay(50).Wait();
。 - stuartdWait
,因为在异步操作上阻塞时可能会出现死锁。 - Craig使用此代码
using System.Threading;
// ...
Thread.Sleep(50);
Thread.Sleep(50);
线程将不会按照指定的时间被操作系统调度执行。该方法改变线程的状态以包括WaitSleepJoin。
该方法不执行标准的COM和SendMessage泵。如果您需要在具有STAThreadAttribute的线程上睡眠,但想执行标准的COM和SendMessage泵,请考虑使用指定超时间隔的Join方法的重载之一。
Thread.Join
为了阅读易懂:
using System.Threading;
Thread.Sleep(TimeSpan.FromMilliseconds(50));
从.NET Framework 4.5开始,您可以使用:
最初的回答:
using System.Threading.Tasks;
Task.Delay(50).Wait(); // wait 50ms
using System;
using System.Threading;
class Program
{
static void Main()
{
// Sleep for 50 milliseconds
Thread.Sleep(50);
Console.WriteLine("Sleep for 50 milliseconds");
}
}
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// Sleep for 50 milliseconds asynchronously
await Task.Delay(50);
// pause for 50 milliseconds and then continue execution.
Console.WriteLine("resumed after sleeping for 50 ms.");
}
}
两全其美:
using System.Runtime.InteropServices;
[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod", SetLastError = true)]
private static extern uint TimeBeginPeriod(uint uMilliseconds);
[DllImport("winmm.dll", EntryPoint = "timeEndPeriod", SetLastError = true)]
private static extern uint TimeEndPeriod(uint uMilliseconds);
/**
* Extremely accurate sleep is needed here to maintain performance so system resolution time is increased
*/
private void accurateSleep(int milliseconds)
{
//Increase timer resolution from 20 miliseconds to 1 milisecond
TimeBeginPeriod(1);
Stopwatch stopwatch = new Stopwatch();//Makes use of QueryPerformanceCounter WIN32 API
stopwatch.Start();
while (stopwatch.ElapsedMilliseconds < milliseconds)
{
//So we don't burn cpu cycles
if ((milliseconds - stopwatch.ElapsedMilliseconds) > 20)
{
Thread.Sleep(5);
}
else
{
Thread.Sleep(1);
}
}
stopwatch.Stop();
//Set it back to normal.
TimeEndPeriod(1);
}