我正在运行此代码;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var task = DoSomething();
Task.WaitAny(task);
Console.WriteLine(task.Result);
Console.Write("Press any key");
Console.ReadKey();
}
static void WriteLine(string str, params string[] param)
{
Console.WriteLine(str, param);
System.Diagnostics.Debug.WriteLine(str, param);
}
async static Task<string> DoSomething()
{
int timeout = 20;
int count = 1;
CancellationTokenSource cts;
string output;
do
{
cts = new CancellationTokenSource(timeout);
//does also happen with
//output = await Task<string>.Run(() => test(count, timeout, cts.Token));
output = await Task<string>.Run(() => test(count, timeout, cts.Token), cts.Token);
if (cts.IsCancellationRequested)
{
WriteLine(string.Format("Retry. Count value {2}, Test Sleep time {0}, Method timeout {1}", output, timeout, count));
timeout += 50;
count++;
}
} while (cts.IsCancellationRequested);
return string.Format("Count value {2}, Test Sleep time {0}, Method timeout {1}", output, timeout, count);
}
async static Task<string> test(int count, int timeout, CancellationToken ct)
{
int sleep = 400 - (count * 5);
await Task.Run(() => Task.Delay(sleep), ct);
if (!ct.IsCancellationRequested)
{
WriteLine(string.Format("Succeed. Count value {2}, Test Sleep time {0}, Method timeout {1}", sleep, timeout, count));
}
return sleep.ToString();
}
}
}
我得到了这个输出
Retry. Count value 1, Test Sleep time 395, Method timeout 20
Retry. Count value 2, Test Sleep time 390, Method timeout 70
Retry. Count value 3, Test Sleep time 385, Method timeout 120
Retry. Count value 4, Test Sleep time 380, Method timeout 170
Retry. Count value 5, Test Sleep time 375, Method timeout 220
Retry. Count value 6, Test Sleep time 370, Method timeout 270
Retry. Count value 7, Test Sleep time 365, Method timeout 320
Succeed. Count value 8, Test Sleep time 360, Method timeout 370
Retry. Count value 8, Test Sleep time 360, Method timeout 370
Succeed. Count value 9, Test Sleep time 355, Method timeout 420
如何在Count value 8
解决问题?
应该说Succeed并停在那里。
答案 0 :(得分:1)
问题是竞争条件,因为IsCancellationRequested
可以在您的成功期间从false
更改为true
。和&#34;重试&#34;校验。在不知道此代码的实际上下文的情况下很难建议正确的修复。