public void TestExceptions3() { TaskExceptionHandler.SetExceptionHandler(task => Console.WriteLine($"[UnhandledException] {task.Exception?.GetBaseException().Message}")); for (var i = 0; i < 5; i++) { Exceptions(i); } void Exceptions(int idx) { switch (idx) { case 0: AsTask.ToMainContext(() => throw new ApplicationException("ToMainContext")); break; case 1: AsTask.ToBackgroundContext(() => throw new ApplicationException("ToBackgroundContext")); break; case 2: AsTask.ToContext(_contextTests2Id, () => throw new ApplicationException("ToContext")); break; case 3: AsTask.ToStaticThreadPool(() => throw new ApplicationException("ToStaticThreadPool")); break; case 4: AsTask.ToDynamicThreadPool(() => throw new ApplicationException("ToDynamicThreadPool")); break; } } }
public async Task TestToBackgroundContext() { await AsTask.ToBackgroundContext(); Console.WriteLine(AsTask.WhereAmI()); Assert.True(AsTask.IsBackgroundContext(), "This is not the BackgroundContext!"); }
private static async Task <long> FindPrimeNumberAsync(int n) { // Here we switch to a normal thread pool to do the heavy work... await AsTask.ToStaticThreadPool(); var count = 0; long a = 2; while (count < n) { long b = 2; var prime = 1; while (b * b <= a) { if (a % b == 0) { prime = 0; break; } b++; } if (prime > 0) { count++; } a++; } return(--a); }
public async Task TestToDynamicThreadPool1() { await AsTask.ToDynamicThreadPool(); Console.WriteLine(AsTask.WhereAmI()); Assert.True(AsTask.IsDynamicThreadPool(), "This is not the DynamicThreadPool!"); }
public async Task TestToAsyncContext() { await AsTask.ToContext(_contextTests1Id); Console.WriteLine(AsTask.WhereAmI()); Assert.True(AsTask.IsThreadContext(_contextTests1Id), $"This is not the AsContext({_contextTests1Id})!"); }
public async ValueTask TestSwitch3() { for (var i = 0; i < 100; i++) { await AsTask.ToDynamicThreadPool(); } }
public async ValueTask TestSwitch1() { for (var i = 0; i < 100; i++) { await AsTask.ToBackgroundContext(); } }
public async ValueTask TestStp2() { await AsTask.ToStaticThreadPool(); await Task.Delay(10); await Task.Yield(); }
public async ValueTask TestBc2() { await AsTask.ToBackgroundContext(); await Task.Delay(10); await Task.Yield(); }
public async ValueTask TestDtp2() { await AsTask.ToDynamicThreadPool(); await Task.Delay(10); await Task.Yield(); }
public async ValueTask TestSwitch4() { for (var i = 0; i < 100; i++) { await AsTask.ToBackgroundContext(); await AsTask.ToStaticThreadPool(); await AsTask.ToDynamicThreadPool(); } }
public async Task TestAwaitPost() { await AsTask.ToMainContext(); var stopwatch = Stopwatch.StartNew(); await AsTask.ToBackgroundContext(() => Thread.Sleep(1000)); stopwatch.Stop(); Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 1000ms"); Assert.True(stopwatch.ElapsedMilliseconds >= 1000); }
public async Task TestDelayAwaiterMainContext() { await AsTask.ToMainContext(); var stopwatch = Stopwatch.StartNew(); await Task.Delay(100); stopwatch.Stop(); Assert.True(AsTask.IsMainContext(), $"This is not the MainContext: {AsTask.WhereAmI()}"); Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms"); Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150); }
public async Task TestExceptions2() { for (var i = 0; i < 5; i++) { try { await Exceptions(i); } catch (Exception) { Console.WriteLine($"Catch exception[{i}]"); } } async Task Exceptions(int idx) { switch (idx) { case 0: await AsTask.ToMainContext(() => throw new ApplicationException("ToMainContext")); break; case 1: await AsTask.ToBackgroundContext(() => throw new ApplicationException("ToBackgroundContext")); break; case 2: await AsTask.ToContext(_contextTests2Id, () => throw new ApplicationException("ToContext")); break; case 3: await AsTask.ToStaticThreadPool(() => throw new ApplicationException("ToStaticThreadPool")); break; case 4: await AsTask.ToDynamicThreadPool(() => throw new ApplicationException("ToDynamicThreadPool")); break; } } }
public async Task TestSwitchingToContexts() { await AsTask.ToBackgroundContext(); Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext); await AsTask.ToContext(_contextTests1Id); Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext); await AsTask.ToStaticThreadPool(); Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.StaticThreadPool); await AsTask.ToDynamicThreadPool(); Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.DynamicThreadPool); }
public async Task TestMoreSwitching() { for (var i = 0; i < 1000; i++) { await TestSwitchingToContexts(); } for (var i = 0; i < 1000; i++) { await AsTask.ToMainContext(); Assert.True(AsTask.IsMainContext()); } for (var i = 0; i < 1000; i++) { await AsTask.ToBackgroundContext(); Assert.True(AsTask.IsBackgroundContext()); } for (var i = 0; i < 1000; i++) { await AsTask.ToContext(_contextTests1Id); Assert.True(AsTask.IsThreadContext(_contextTests1Id)); } for (var i = 0; i < 1000; i++) { await AsTask.ToStaticThreadPool(); Assert.True(AsTask.IsStaticThreadPool()); } for (var i = 0; i < 1000; i++) { await AsTask.ToDynamicThreadPool(); Assert.True(AsTask.IsDynamicThreadPool()); } }
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); } }
public AsTaskBench() { AsTask.Initialize(); }
public async ValueTask TestBc1() { await AsTask.ToBackgroundContext(() => Thread.Sleep(10)); await Task.Yield(); }
public async ValueTask TestDtp1() { await AsTask.ToDynamicThreadPool(() => Thread.Sleep(10)); await Task.Yield(); }
public Tests1() { AsTask.Initialize(); _contextTests1Id = AsTask.CreateContext("Tests1"); }