using System; using System.Threading.Tasks; using Bit.Core.Services; namespace Bit.Core.Utilities { public static class TaskExtensions { /// /// Fires a task and ignores any exception. /// See http://stackoverflow.com/a/22864616/344182 /// /// The task to be forgotten. /// Action to be called on exception. public static async void FireAndForget(this Task task, Action onException = null) { try { await task.ConfigureAwait(false); } catch (Exception ex) { LoggerHelper.LogEvenIfCantBeResolved(ex); onException?.Invoke(ex); } } /// /// Fires a task and ignores any exception. /// See http://stackoverflow.com/a/22864616/344182 /// /// The task to be forgotten. /// Action to be called on exception. public static async void FireAndForget(this Task task, Func shouldLogException) { try { await task.ConfigureAwait(false); } catch (Exception ex) when (shouldLogException(ex)) { LoggerHelper.LogEvenIfCantBeResolved(ex); } } } }