0
有没有办法以某种方式创建一种方式睡在一个执行的函数是异步模仿一个thread.sleep?异步执行中的同步睡眠?
比如我有
Workthread run()
{
while (true)
{
//All calls cannot pause inside, async
CallWorkFunction();
CallOtherWorkFuctuion();
Callthirdworkfunction();
thread.sleep(0x10);
curtick++;
}
}
//CallWorkFunction()
if (nexttick - curtick > 0)
return;
foreach (var item in collection.ToList())
{
if (!item.IsReady)
continue;
collection.Remove(item);
ProcessItem(item);
nexttick = curtick + 20;
return;
}
此功能会工作这么多更好,如果nexttick
可以像一个线程Thread.Sleep()
工作不停止执行。由于异步代码正在出现一系列基于状态的检查,其中许多可以用正确的暂停来替换。
你是想查询的东西每16毫秒?你的'run()'方法看起来应该是一个定时器而不是一个睡眠循环。想想看,整个事情看起来应该在计时器上。尽管没有更多的上下文,但很难说清楚。 –
如果您使用async/await,则可以等待Task.Delay。 –