Skip to content

Instantly share code, notes, and snippets.

@pavelsavara
Created August 21, 2024 16:15
Show Gist options
  • Save pavelsavara/8bdd069170a1230b85bbdc54e1f6fca0 to your computer and use it in GitHub Desktop.
Save pavelsavara/8bdd069170a1230b85bbdc54e1f6fca0 to your computer and use it in GitHub Desktop.
public class Prog
{
static void TimerHandler(object _)
{
Console.WriteLine("TimerHandler A");
}
static void SetNextTimer()
{
Console.WriteLine("SetNextTimer A");
var task = Task.Delay(100);
// note that the continuation is not awaited and nothing is keeping the Task alive from GC
task.ContinueWith(TimerHandler, TaskScheduler.Default);
}
public static async Task<int> Main(string[] args)
{
Console.WriteLine("Main A");
SetNextTimer();
Console.WriteLine("Main B");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Main C");
await Task.Delay(200);
GC.Collect();
GC.WaitForPendingFinalizers();
await Task.Delay(200);
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Main D");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment