public async Task Synchronous() { Console.WriteLine(nameof(Synchronous)); var queue = TaskQueueFactory.Create <TaskInfo>("1").Start(); int taskExecutedCount = 0; await queue.Schedule("Task 1", (context, ct) => { Console.WriteLine("Item 1"); taskExecutedCount++; }).Schedule("Task 2", (context, ct) => { Console.WriteLine("Item 2"); taskExecutedCount++; }).Schedule("Task 3", (context, ct) => { Console.WriteLine("Item 3"); taskExecutedCount++; }).AwaitIsEmpty(); Assert.Equal(3, taskExecutedCount); }
public async Task Combo() { Console.WriteLine(nameof(Combo)); var queue = TaskQueueFactory.Create <TaskInfo>("3").Start(); int taskExecutedCount = 0; await queue.Schedule("Task 1", async (context, ct) => { Console.WriteLine("Item 1: Async"); await Task.Delay(2000); taskExecutedCount++; }).Schedule("Task 2", (context, ct) => { Console.WriteLine("Item 2: Sync"); taskExecutedCount++; }).Schedule("Task 3", async(context, ct) => { Console.WriteLine("Item 3: Async"); await Task.Delay(2000); taskExecutedCount++; }).AwaitIsEmpty(); Assert.Equal(3, taskExecutedCount); }
public async Task Run2() { Console.WriteLine(nameof(Run2)); var queue = TaskQueueFactory.Create <TaskInfo>("7"); queue.CancelOnException = true; await queue .Schedule((context, ct) => Console.WriteLine("Hello World!")) .Start() .AwaitIsEmpty(); }
public async Task Exception_EventHandler_CancelOverride() { Console.WriteLine(nameof(Exception_EventHandler_CancelOverride)); string tag = null; int taskExecutedCount = 0; var queue = TaskQueueFactory.Create <TaskInfo>("5"); queue.CancelOnException = true; queue.Start(); queue.TaskException += (sender, args) => { tag = args.Task.Tag; Console.WriteLine($"Exception in \"{args.Task.Tag}\":\n\n{args.Exception}"); args.Cancel = false; }; queue.Schedule("Task 1", async(context, ct) => { Console.WriteLine("Item 1: Async"); await Task.Delay(2000); taskExecutedCount++; }).Schedule("Task 2", (context, ct) => { Console.WriteLine("Item 2: Sync"); taskExecutedCount++; throw new Exception(); }).Schedule("Task 3", async(context, ct) => { Console.WriteLine("Item 3: Async"); await Task.Delay(2000); taskExecutedCount++; }); await queue.AwaitIsEmpty(); Assert.Equal(3, taskExecutedCount); Assert.Equal("Task 2", tag); }
public async Task Run3() { Console.WriteLine(nameof(Run3)); var queue = TaskQueueFactory.Create <TaskInfo>("8"); queue.CancelOnException = true; queue.Started += (s, e) => Console.WriteLine("Started"); queue.Stopped += (s, e) => Console.WriteLine("Stopped"); await queue.Schedule((context, ct) => Console.WriteLine("Task 1")).Schedule((context, ct) => { queue.Stop(); Console.WriteLine("Task 2"); }) .Schedule((context, ct) => Console.WriteLine("Task 3")) .Start() .AwaitIsStopped(); }
public async Task Run() { Console.WriteLine(nameof(Run)); var queue = TaskQueueFactory.Create <TaskInfo>("6"); queue.CancelOnException = true; queue.Start(); queue.TaskException += (s, e) => { Console.WriteLine(e.Task.Tag); e.Cancel = false; }; for (int i = 1; i <= 5; i++) { int x = i; queue.Schedule($"Task: {x}", async(context, ct) => { if (x == 2) { throw new Exception(); } Console.WriteLine($"Task: {x} ({queue.Count} tasks left)"); var task = queue.GetCurrentTask(); Console.WriteLine($"Task!!! {task.Tag}"); await Task.Delay(2000); }); } await queue.AwaitIsEmpty(); }
public async Task FailedToAdd() { long sum = 0; var queue = new TaskQueueFactory().Create <int>((x, cancellationToken) => { Interlocked.Add(ref sum, x); return(Task.FromResult(0)); }, Environment.ProcessorCount, Environment.ProcessorCount); var n = 1000; using (var cancelSource = new CancellationTokenSource(TimeSpan.FromSeconds(5))) { var task = Task.Run(() => queue.RunAsync(cancelSource.Token)); for (var i = 0; i < n; i++) { queue.Add(i); } await task.ContinueWith(_ => { }); Assert.NotEqual((n * (n - 1)) / 2, sum); } }