gpt4 book ai didi

c# - 等待运算符的奇怪问题

转载 作者:行者123 更新时间:2023-12-02 21:36:38 26 4
gpt4 key购买 nike

今天我遇到了一个奇怪的问题...我的代码在 C# 中的等待方法上运行(没有错误或其他)。

user = await JsonConvert.DeserializeObjectAsync<User>(content);

但是当我删除“等待”时,我的代码可以工作......

user = JsonConvert.DeserializeObjectAsync<User>(content).Result;

我的 HttpClient 也有同样的问题:

作品

using (HttpResponseMessage response = client.PostAsync(url, posts).Result)

不起作用

using (HttpResponseMessage response = await client.PostAsync(url, posts))

问题前一天不存在,如果我将代码放在另一个类中,它可以正常工作

输出:

The thread 0xe44 has exited with code 259 (0x103).
The thread 0x81c has exited with code 259 (0x103).
The thread 0x150c has exited with code 259 (0x103).
The thread 0x1660 has exited with code 259 (0x103).

我重建了,但仍然有这个问题。

有人知道答案吗?

最佳答案

您遇到了common deadlock issue我在博客上描述过。

要解决这个问题,请使用 await一路走来;删除任何 Task.WaitTask<T>.Result来自您的代码的调用。

关于c# - 等待运算符的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245929/

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