我使用HTTPClient处理这个问题已有一段时间了。我的HTTPClient异步方法每隔几个小时就会不断抛出TaskCanceledException。
我已经检查过IsCancellationRequested并确认令牌已取消,所以这不是超时问题。
https://i.imgur.com/QdCPcFk.png
该异常始终被抛出以下GetAsync方法:
public async Task CheckDodgeOrStart() // checks if game started
{
while (true)
{
try
{
var response = await client.GetAsync(_baseURL + /lol-gameflow/v1/gameflow-phase");
var stringResponse = await response.Content.ReadAsStringAsync();
if (stringResponse.Contains("InProgress") || stringResponse.Contains("Reconnect"))
{
if (bot.isLeader)
{
Form1.Log(_username + ": " + "Game has successfully started..");
}
break;
}
await Task.Delay(1000);
}
catch (TaskCanceledException e)
{
if (!e.CancellationToken.IsCancellationRequested)
Console.WriteLine("Timeout");
}
}
}
非常感谢您的帮助。