public AsTaskBench() { AsTask.Initialize(); }
public Tests1() { AsTask.Initialize(); _contextTests1Id = AsTask.CreateContext("Tests1"); }
private static async Task Main() { WriteLine("Assign an exception handler."); TaskExceptionHandler.SetExceptionHandler(task => { Error.WriteLine(task.Exception != null ? $"[ExceptionHandler] {task.Exception.GetBaseException().Message}" : $"[ExceptionHandler] Unhandled exception in task {task}"); }); WriteLine("We get or create a synchronization context and switch to it."); await AsTask.Initialize(); WriteLine($"Print to the console information about the current context: {AsTask.WhereAmI()}"); WriteLine("Switch to the background context."); await AsTask.ToBackgroundContext(); WriteLine($"Now we get information about the context of the background context: {AsTask.WhereAmI()}"); WriteLine("Back switch to the main context."); await AsTask.ToMainContext(); WriteLine("We call the asynchronous methods, which performs the heavy work..."); var tasks = new List <Task <long> >(); for (var i = 1; i <= 10; i++) { tasks.Add(FindPrimeNumberAsync(20000 * i)); } WriteLine("Asynchronously waiting for the execution of tasks."); _ = Task.WhenAll(tasks).ContinueWith(async task => { await AsTask.ToMainContext(); WriteLine("This is the result of our calculations:"); foreach (var val in task.Result) { WriteLine(val); } Shutdown(); }).ExceptionHandler(); if (AsTask.IsMainContext()) { WriteLine("hmm, we're still in the main context!:)"); } WriteLine("The life cycle of a console application runs on the main context."); var scheduler = AsTask.GetStaticTaskScheduler(); while (!_isShutdown) { WriteLine($"Count running tasks: {scheduler.CountExecutableTasks}; Count tasks in queue: {scheduler.CountTasksInQueue}"); await Task.Delay(1000); // Each iteration waits asynchronously for 1000 ms } WriteLine("Shutdown..."); for (var i = 5; i > 0; i--) { WriteLine($"Shutdown through {i}s"); await Task.Delay(1000); } }