Is it possible to cancel all async methods, without knowing what is currently running?
For example I have several classes that may run async tasks:
class Class1 { public async void SomeTask() { for (int i = 0; i < 5; i++) { // Doing some job await Task.Delay(2000); } } } class Class2 { public async void ContinuouslyTask() { for (;;) { // Doing some job on background await Task.Delay(1000); } } } And I want turn off every async task, before I will logout:
class Program { static void Main(string[] args) { var p = new Program(); var c1 = new Class1(); var c2 = new Class2(); c1.SomeTask(); c2.ContinuouslyTask(); while (Console.ReadKey().Key != ConsoleKey.Enter) { } p.Logout(); } private void Logout() { // Cancel all async tasks // And do logout work } } Is it possible to do this, without saving tasks into query?