2019-10-30 19:39:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-06-26 17:54:44 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-10-30 19:39:12 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public static class TaskExt
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public static async void FireAndForget(this Task task, Action<Exception>? onException = null)
|
2019-10-30 19:39:12 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await task.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-06-26 17:54:44 +00:00
|
|
|
|
if (onException == null)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error(ex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
onException(ex);
|
|
|
|
|
}
|
2019-10-30 19:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-23 05:43:15 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns a Task that will await the input task, but fire an action if it takes longer than a given time
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task TimeoutButContinue(this Task task, TimeSpan timeout, Action actionOnTimeout)
|
|
|
|
|
{
|
|
|
|
|
var timeoutTask = Task.Delay(timeout);
|
|
|
|
|
var completedTask = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);
|
|
|
|
|
if (completedTask == timeoutTask)
|
|
|
|
|
{
|
|
|
|
|
actionOnTimeout();
|
|
|
|
|
await task.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns a Task that will await the input task, but fire an action if it takes longer than a given time
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task<TRet> TimeoutButContinue<TRet>(this Task<TRet> task, TimeSpan timeout, Action actionOnTimeout)
|
|
|
|
|
{
|
|
|
|
|
var timeoutTask = Task.Delay(timeout);
|
|
|
|
|
var completedTask = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);
|
|
|
|
|
if (completedTask == timeoutTask)
|
|
|
|
|
{
|
|
|
|
|
actionOnTimeout();
|
|
|
|
|
}
|
|
|
|
|
return await task.ConfigureAwait(false);
|
|
|
|
|
}
|
2019-10-30 19:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|