39 lines
1.2 KiB
C#
Raw Normal View History

2019-04-29 17:42:09 +02:00
using System;
using System.Threading;
using System.Threading.Tasks;
2021-08-27 11:03:47 -04:00
namespace PluralKit.Core
{
public static class TaskUtils
{
public static async Task CatchException(this Task task, Action<Exception> handler)
{
try
{
2019-04-29 17:42:09 +02:00
await task;
2021-08-27 11:03:47 -04:00
}
catch (Exception e)
{
2019-04-29 17:42:09 +02:00
handler(e);
}
}
2021-08-27 11:03:47 -04:00
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan? timeout)
{
2019-04-29 17:42:09 +02:00
// https://stackoverflow.com/a/22078975
2021-08-27 11:03:47 -04:00
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
{
2019-04-29 17:42:09 +02:00
var completedTask = await Task.WhenAny(task, Task.Delay(timeout ?? TimeSpan.FromMilliseconds(-1), timeoutCancellationTokenSource.Token));
2021-08-27 11:03:47 -04:00
if (completedTask == task)
{
2019-04-29 17:42:09 +02:00
timeoutCancellationTokenSource.Cancel();
return await task; // Very important in order to propagate exceptions
2021-08-27 11:03:47 -04:00
}
else
{
2019-04-29 17:42:09 +02:00
throw new TimeoutException();
}
}
}
}
}