我有一个方法,它使用任务和async / await重复工作.
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Action action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return new Task(action, SomeCt, TaskCreationOptions.LongRunning);
}
我写过相应的测试:
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
task.Start();
await task;
}
我期待这个测试失败,但它通过(并崩溃测试运行器).
但是,如果在ToRepeatingWork方法中,我将操作从异步更改为正常操作并使用等待而不是等待,则测试将按预期运行.
TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds)).Wait();
这有什么不对?
最佳答案 你永远不应该使用任务构造函数.如果您有工作要放在线程池上,请使用Task.Run.这是一个问题,但不是导致崩溃的原因.
您还应该避免异步void,因此请使用Func< Task>而不是行动.这就是导致崩溃的原因.
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Func<Task> action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return Task.Run(() => action());
}
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
await task;
}