监视超时的同步方法

And*_*dre 14 c# asynchronous

如果同步方法执行时间太长,我正在寻找一种有效的方法来抛出超时异常.我见过一些样品,但没有什么能完全符合我的要求.

我需要做的是

  1. 检查同步方法是否超过其SLA
  2. 如果它确实抛出超时异常

不是要终止同步方法,如果执行时间过长.(多次故障会使断路器跳闸并防止级联故障)

到目前为止我的解决方案如下所示.请注意,我确实将CancellationToken传递给了sync方法,希望它能在超时时遵循取消请求.此外,我的解决方案返回一个任务,然后我可以根据需要等待我的调用代码.

我担心的是,这个代码会为每个监控方法创建两个任务.我认为TPL会很好地管理这个,但我想证实一下.

这有意义吗?有一个更好的方法吗?

private Task TimeoutSyncMethod( Action<CancellationToken> syncAction, TimeSpan timeout )
{
  var cts = new CancellationTokenSource();

  var outer = Task.Run( () =>
  {
     try
     {
        //Start the synchronous method - passing it a cancellation token
        var inner = Task.Run( () => syncAction( cts.Token ), cts.Token );

        if( !inner.Wait( timeout ) )
        {
            //Try give the sync method a chance to abort grecefully
            cts.Cancel();
            //There was a timeout regardless of what the sync method does - so throw
            throw new TimeoutException( "Timeout waiting for method after " + timeout );
        }
     }
     finally
     {
        cts.Dispose();
     }
  }, cts.Token );

  return outer;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

使用@Timothy的回答我现在正在使用它.虽然代码没有明显减少,但它更清晰.谢谢!

  private Task TimeoutSyncMethod( Action<CancellationToken> syncAction, TimeSpan timeout )
  {
    var cts = new CancellationTokenSource();

    var inner = Task.Run( () => syncAction( cts.Token ), cts.Token );
    var delay = Task.Delay( timeout, cts.Token );

    var timeoutTask = Task.WhenAny( inner, delay ).ContinueWith( t => 
      {
        try
        {
          if( !inner.IsCompleted )
          {
            cts.Cancel();
            throw new TimeoutException( "Timeout waiting for method after " + timeout );
          }
        }
        finally
        {
          cts.Dispose();
        }
      }, cts.Token );

    return timeoutTask;
  }
Run Code Online (Sandbox Code Playgroud)

Tim*_*lds 20

如果你有一个Task被叫task,你可以这样做:

var delay = Task.Delay(TimeSpan.FromSeconds(3));
var timeoutTask = Task.WhenAny(task, delay);
Run Code Online (Sandbox Code Playgroud)

如果timeoutTask.Result结束了task,那么它没有超时.否则,它delay就会超时.

我不知道这是否会与你实现的内容完全相同,但它是内置的方法.