public static void Explain(this UnobservedException runnable, TextWriter writer) { writer.WriteLine(@" - Async void methods will crash the process if an exception is thrown - `Task`-returning methods are better since unhandled exceptions trigger the `TaskScheduler.UnobservedTaskException` - Be ware that only when the finalizers are run the unobserved exception is thrown "); }
private void onUnobservedException(Exception e) { if (Debugger.IsAttached) { Debugger.Break(); } var eventArgs = new UnobservedTaskExceptionEventArgs(new AggregateException(e)); UnobservedException?.Invoke(this, eventArgs); if (!eventArgs.Observed) { Environment.FailFast("Unobserved exception in Dbus handling", e); } }
public static void Explain(this UnobservedException runnable, TextWriter writer) { writer.WriteLine(@" - Only when the finalizers are run the unobserved exception is thrown "); }
private async Task ExecuteOnceAsync(CancellationToken token) { var taskFactory = new TaskFactory(TaskScheduler.Current); // Cached since this will be referenced quite frequently in this method var referenceTime = DateTime.Now; // Determine which tasks will never run again and remove them from the database var removeList = _scheduledTasks.Where(x => x.Value.WillNeverRunAgain).ToList(); // We must ensure that our database is updated -- to remove tasks that will never run again using (DevryDbContext context = new DevryDbContext()) { foreach (var pair in removeList) { Models.Reminder reminder = await context.Reminders.FindAsync(pair.Key); if (reminder == null) { _logger.LogWarning($"Unable to locate reminder with Id: {pair.Key}"); continue; } else { _logger.LogInformation($"Reminder: '{reminder.Name}' with Id '{reminder.Id}' is being cleaned up. --Determined to never run again"); context.Reminders.Remove(reminder); _scheduledTasks.Remove(pair.Key); } } // Save changes (if applicable) await context.SaveChangesAsync(); } var tasksThatShouldRun = _scheduledTasks.Values.Where(x => x.ShouldRun(referenceTime)); foreach (var task in tasksThatShouldRun) { task.Increment(); await taskFactory.StartNew( async() => { try { await task.Task.ExecuteAsync(token); } catch (Exception ex) { var args = new UnobservedTaskExceptionEventArgs(ex as AggregateException ?? new AggregateException(ex)); UnobservedException?.Invoke(this, args); if (!args.Observed) { throw; } } }, token); } }