Skip to content

Instantly share code, notes, and snippets.

@noseratio
Created November 25, 2018 11:57
Show Gist options
  • Save noseratio/7cacaff95e791932594b16fd02a36ed0 to your computer and use it in GitHub Desktop.
Save noseratio/7cacaff95e791932594b16fd02a36ed0 to your computer and use it in GitHub Desktop.
TaskScheduler.UnobservedTaskException - https://stackoverflow.com/a/23505426/1768303
using System;
using System.Linq;
using System.Threading.Tasks;
namespace UnobservedTaskExceptionDemo
{
class Program
{
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException += (s, e) =>
{
Console.WriteLine("UnobservedTaskException");
};
Enumerable.Range(1, 10).Select(i =>
Task.Run(() => { if (i % 2 != 0) throw new InvalidOperationException(); })).ToArray();
Console.WriteLine("Before GC...");
Console.ReadKey();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
Console.WriteLine("After GC...");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment