This commit is contained in:
2020-05-25 20:58:49 +00:00
parent 5e4fa54f5e
commit ef106784d4

View File

@@ -0,0 +1,24 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AyaNova.Util
{
internal static class TaskUtil
{
///<summary>
///Usage:
///await WithTimeoutAfterStart(ct => SomeOperationAsync(ct), TimeSpan.FromMilliseconds(n));
///</summary>
public static async Task WithTimeoutAfterStart(Func<CancellationToken, Task> operation, TimeSpan timeout)
{
//https://stackoverflow.com/a/23478628/8939
var source = new CancellationTokenSource();
var task = operation(source.Token);
source.CancelAfter(timeout);
await task;
}
}//eoc
}//eons