using System;
using System.Threading.Tasks;
namespace Connected
{
public enum TaskOption
{
None,
Safe
}
public static class TaskExtensions
{
///
/// Task will be awaited and exceptions will be logged to console (TaskOption.Safe) or managed by the Blazor framework (TaskOption.None).
///
public static async void AndForget(this Task task, TaskOption option = TaskOption.None)
{
try
{
await task;
}
catch (Exception ex)
{
if (option != TaskOption.Safe)
throw;
Console.WriteLine(ex);
}
}
///
/// ValueTask will be awaited and exceptions will be logged to console (TaskOption.Safe) or managed by the Blazor framework (TaskOption.None).
///
public static async void AndForget(this ValueTask task, TaskOption option = TaskOption.None)
{
try
{
await task;
}
catch (Exception ex)
{
if (option != TaskOption.Safe)
throw;
Console.WriteLine(ex);
}
}
///
/// ValueTask(bool) will be awaited and exceptions will be logged to console (TaskOption.Safe) or managed by the Blazor framework (TaskOption.None).
///
public static async void AndForget(this ValueTask task, TaskOption option = TaskOption.None)
{
try
{
await task;
}
catch (Exception ex)
{
if (option != TaskOption.Safe)
throw;
Console.WriteLine(ex);
}
}
}
}