From ef106784d44b6175da833626d47d1cf1255cff2f Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 25 May 2020 20:58:49 +0000 Subject: [PATCH] --- server/AyaNova/util/TaskUtil.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/AyaNova/util/TaskUtil.cs diff --git a/server/AyaNova/util/TaskUtil.cs b/server/AyaNova/util/TaskUtil.cs new file mode 100644 index 00000000..72533501 --- /dev/null +++ b/server/AyaNova/util/TaskUtil.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace AyaNova.Util +{ + internal static class TaskUtil + { + /// + ///Usage: + ///await WithTimeoutAfterStart(ct => SomeOperationAsync(ct), TimeSpan.FromMilliseconds(n)); + /// + public static async Task WithTimeoutAfterStart(Func 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 \ No newline at end of file