如何替换异步代码中的无限睡眠?
How to replace infinite sleep in async code?
我在服务结构中有初始化服务并永远等待的代码(它来自 VS 生成的模板)。我将此 GetAwaiter().GetResult()
替换为 async await
,但我不知道如何处理 Thread.Sleep
- Task.Delay
我认为 TimeSpan 的最大值会在某个时候结束。
ServiceRuntime.RegisterServiceAsync("Stateless1Type",
context => new Stateless1(context)).GetAwaiter().GetResult();
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
试试这个:
await Task.Delay(-1);
The number of milliseconds to wait before completing the returned
task, or -1 to wait indefinitely.
我在服务结构中有初始化服务并永远等待的代码(它来自 VS 生成的模板)。我将此 GetAwaiter().GetResult()
替换为 async await
,但我不知道如何处理 Thread.Sleep
- Task.Delay
我认为 TimeSpan 的最大值会在某个时候结束。
ServiceRuntime.RegisterServiceAsync("Stateless1Type",
context => new Stateless1(context)).GetAwaiter().GetResult();
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
试试这个:
await Task.Delay(-1);
The number of milliseconds to wait before completing the returned task, or -1 to wait indefinitely.