Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created March 13, 2018 05:37
Show Gist options
  • Save azyobuzin/ea8785c7f3de171d4ff30a827bccb230 to your computer and use it in GitHub Desktop.
Save azyobuzin/ea8785c7f3de171d4ff30a827bccb230 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
Console.WriteLine(e.Exception.ToString());
};
// var task1 = Task.Run(() => throw new Exception());
// var task2 = task1.ContinueWith
// と書くと、 task1 が GC されないので UnobservedTaskException が発生しない
var task = Task.Run(() => throw new Exception())
.ContinueWith(t => Console.WriteLine("hoge"));
task.Wait();
Console.WriteLine(task.Status);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment