source code checkin

This commit is contained in:
Brett Hewitson 2021-01-07 16:23:23 +10:00
parent 5f8fb2c825
commit 7e57b72e35
675 changed files with 168433 additions and 0 deletions

View file

@ -0,0 +1,60 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace ServerManagerTool.Common.Utils
{
public static class TaskUtils
{
public static void DoNotWait(this Task task)
{
// Do nothing, let the task continue. Eliminates compiler warning about non-awaited tasks in an async method.
}
public static async Task RunOnUIThreadAsync(Action action)
{
var app = Application.Current;
if (app != null)
{
await app.Dispatcher.InvokeAsync(action);
}
}
public static readonly Task FinishedTask = Task.FromResult(true);
public static async Task TimeoutAfterAsync(this Task task, int millisecondsDelay)
{
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
{
var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
await task; // Very important in order to propagate exceptions
}
else
{
throw new TimeoutException("The operation has timed out.");
}
}
}
public static async Task<TResult> TimeoutAfterAsync<TResult>(this Task<TResult> task, int millisecondsDelay)
{
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
{
var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
return await task; // Very important in order to propagate exceptions
}
else
{
throw new TimeoutException("The operation has timed out.");
}
}
}
}
}