gpt4 book ai didi

c# - 异步总是 WaitingForActivation

转载 作者:IT王子 更新时间:2023-10-29 03:51:50 25 4
gpt4 key购买 nike

我试图找出 asyncawait 关键字的全部内容,但是输出不是我所期望的。

控制台应用如下:

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Foo called");
var result = Foo(5);

while (result.Status != TaskStatus.RanToCompletion)
{
Console.WriteLine("Thread ID: {0}, Status: {1}", Thread.CurrentThread.ManagedThreadId, result.Status);
Task.Delay(100).Wait();
}

Console.WriteLine("Result: {0}", result.Result);
Console.WriteLine("Finished.");
Console.ReadKey(true);
}

private static async Task<string> Foo(int seconds)
{
return await Task.Run(() =>
{
for (int i = 0; i < seconds; i++)
{
Console.WriteLine("Thread ID: {0}, second {1}.", Thread.CurrentThread.ManagedThreadId, i);
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}

return "Foo Completed.";
});
}
}

输出是:

Foo called
Thread ID: 10, Status: WaitingForActivation
Thread ID: 6, second 0.
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 6, second 1.
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 6, second 2.
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 6, second 3.
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 6, second 4.
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Thread ID: 10, Status: WaitingForActivation
Result: Foo Completed.
Finished..

我期望在方法启动后看到状态从 WaitingForActivation 改变。

它如何保持这种状态并处于事件状态?

最佳答案

对于我的回答,值得记住的是 TPL ( Task-Parallel-Library )、Task 类和 TaskStatus 枚举是在 async-await 关键字和async-await 关键字不是 TPL 的最初动机。

在标记为 async 的方法上下文中,生成的 Task 不是表示方法执行的 Task,而是一个 Task 方法的延续。

这只能使用几种可能的状态:

  • 取消
  • 有问题
  • 随机完成
  • 等待激活

我知道 Running 似乎是比 WaitingForActivation 更好的默认值,但是这可能会产生误导,因为大多数时候,异步方法是 < em>executed 实际上并没有运行(即它可能是 await-ing 其他东西)。另一个选项可能是向 TaskStatus 添加一个新值,但这可能是对现有应用程序和库的重大更改。

所有这些都与使用作为原始 TPL 一部分的 Task.Run 非常不同,它能够使用 TaskStatus 的所有可能值 枚举。

如果您希望跟踪异步方法的状态,请查看 IProgress(T)界面,这将允许您报告正在进行的进度。这篇博文,Async in 4.5: Enabling Progress and Cancellation in Async APIs将提供有关使用 IProgress(T) 接口(interface)的更多信息。

关于c# - 异步总是 WaitingForActivation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com