我们目前有一个天真的RetryWrapper,它会在异常发生时重试给定的函数:
public T Repeat<T, TException>(Func<T> work, TimeSpan retryInterval, int maxExecutionCount = 3) where TException : Exception
{
...
对于retryInterval,我们使用以下逻辑在下一次尝试之前进行“等待”。
_stopwatch.Start();
while (_stopwatch.Elapsed <= retryInterval)
{
// do nothing but actuallky it does! lots of CPU usage specially if retryInterval is high
}
_stopwatch.Reset();
我不特别喜欢这个逻辑,理想情况下我希望重试逻辑不要在主线程上发生,你能想到更好的方法吗?
注意:我很乐意考虑 .Net >= 3.5 的答案。