Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MultiThreading and CancellationToken
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; namespace Rextester { public class Program { public static void Main(string[] args) { CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationToken = cancellationTokenSource.Token; List<MyItem> myItems = new List<MyItem>(); myItems.Add(createtask1(2000,1000,cancellationToken)); // this task should run for one second....result: should timeout myItems.Add(createtask2(2000,3000,cancellationToken));// this task should run for two second....result: should NOT timeout Parallel.ForEach(myItems,(item)=>{ Task task = item.T; // start the task task.Start(); Console.WriteLine("waiting for the task with timeout:{0} to finish", item.Timeout); bool hasNotTimedOut = task.Wait(item.Timeout); if (hasNotTimedOut) { Console.WriteLine("Task with timeout: {0}, has not timed out", item.Timeout); } else { Console.WriteLine("Task with timeout: {0}, has timed out", item.Timeout); try { cancellationTokenSource.Cancel(); Console.WriteLine("Status of the task with timeout: {0} is {1}", item.Timeout,task.Status.ToString()); } catch (AggregateException ex) { //if (ex.InnerException is OperationCanceledException) //else // Console.WriteLine(ex); } } }); Task.WaitAll(myItems.Select(item=>item.T).ToArray()); Console.WriteLine("End of Main"); } private static MyItem createtask1(int delayTime, int timeout,CancellationToken cancellationToken) { MyItem toReturn = new MyItem(); toReturn.Timeout = timeout; toReturn.T = new Task(()=>{ Console.WriteLine("Task1 is getting executed with DelayTime:{0}, timeout:{1}",delayTime,timeout); Task.Delay(delayTime).Wait(); Console.WriteLine("Task1 finished executing with DelayTime:{0}, timeout:{1}",delayTime,timeout); cancellationToken.ThrowIfCancellationRequested(); },cancellationToken); return toReturn; } private static MyItem createtask2(int delayTime, int timeout,CancellationToken cancellationToken) { MyItem toReturn = new MyItem(); toReturn.Timeout = timeout; toReturn.T = new Task(()=>{ Console.WriteLine("Task2 is getting executed with DelayTime:{0}, timeout:{1}",delayTime,timeout); Task.Delay(delayTime).Wait(); Console.WriteLine("Task2 finished executing with DelayTime:{0}, timeout:{1}",delayTime,timeout); cancellationToken.ThrowIfCancellationRequested(); },cancellationToken); return toReturn; } } public class MyItem{ public Task T{get;set;} public int Timeout{get;set;} } }
run
|
edit
|
history
|
help
0
condicional "?"
Byte array to base 64 string
Pizzas & Array of Tuples
Bubble Sort
Func Delegate
Move 2 ushort to 1 int
DD
Customised Code Dashboard
timer
Project Euler problem 1