[英]Wait for a while without blocking main thread
我希望我的方法等待大约 500 毫秒,然后检查某些标志是否已更改。 如何在不阻塞应用程序的其余部分的情况下完成此操作?
Thread.Sleep(500)
将强制当前线程等待 500 毫秒。 它可以工作,但如果您的整个应用程序都在一个线程上运行,这不是您想要的。
在这种情况下,您将需要使用Timer
,如下所示:
using System.Timers;
void Main()
{
Timer t = new Timer();
t.Interval = 500; // In milliseconds
t.AutoReset = false; // Stops it from repeating
t.Elapsed += new ElapsedEventHandler(TimerElapsed);
t.Start();
}
void TimerElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello, world!");
}
如果您希望计时器自动重复,您可以将AutoReset
设置为 true(或根本不设置)。
您可以使用await Task.Delay(500);
不会像Sleep
那样阻塞线程,并且代码比 Timer 少很多。
如果有问题的方法与应用程序的其余部分在不同的线程上执行,请执行以下操作:
Thread.Sleep(500);
System.Threading.Thread.Sleep(500);
更新
这不会阻塞应用程序的其余部分,只会阻塞正在运行您的方法的线程。
使用计时器应该可以解决问题
如果您需要使用线程,那么这里是一个示例
void Main()
{
System.Threading.Thread check= new System.Threading.Thread(CheckMethod);
check.Start();
}
private void CheckMethod()
{
//Code
Thread.Sleep(500);
}
异步任务:
var task = new Task (() => function_test()); task.Start();
public void function_test() { `Wait for 5000 miliseconds` Task.Delay(5000);` }
我最近一直在努力解决同样的问题,我需要在不阻塞 UI 的情况下按计划运行操作。
这是我的解决方案:
private void Button_Click(object sender, RoutedEventArgs e)
{
RunOnSchedule(interval, cancellationToken);
}
private void RunOnSchedule(int interval, CancellationToken cancellationToken)
{
// Start the task you want to run on schedule
TaskToRunOnSchedule(args);
Task.Run(async () =>
{
// This loop checks if the task was requested to be cancelled every 1000 ms
for (int x = 0; x < interval; x+=1000)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
await Task.Delay(1000);
}
}).GetAwaiter().OnCompleted(() =>
{
// Once the task for delaying is completed, check once more if cancellation is requested, as you will reach this point regardless of if it was cancelled or not.
if (!cancellationToken.IsCancellationRequested)
{
// Run this method again
RunOnSchedule(interval, cancellationToken);
}
});
}
在 WinForms 应用程序中,当我想在主线程上等待而不阻塞应用程序时,我通常使用
private void Wait (double milliseconds)
{
DateTime next = System.DateTime.Now.AddMilliseconds(milliseconds);
while (next > System.DateTime.Now)
Application.DoEvents();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.