This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task.Factory.StartNew(() => { Console.WriteLine("I am from another ThreadPlanet"); }); //Creating task using Factory | |
Task t1 = new Task(new Action(printMessage)); //Use Action Delegate With Named Method | |
t1.Start(); | |
Task t2 = new Task(delegate { Console.WriteLine("Hello Alien!"); }); //Use an anonymous delegate | |
t2.Start(); | |
Task t3 = new Task(() => printMessage()); //Use Lambda Expression and named method | |
t3.Start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task t1 = new Task(new Action<object>(printMessage), "Task 1"); //Use Action Delegate with Named Method + Setting State | |
t1.Start(); | |
Task t2 = new Task(delegate (Object obj) { Console.WriteLine("Hello Alien! I am from {0}", obj); }, "Task 2"); //Use an anonymous delegate + Setting State | |
t2.Start(); | |
Task t3 = new Task((obj) => printMessage(obj), "Task 3"); //Use Lambda Expression and named method + Setting State | |
t3.Start(); | |
Task t4 = new Task((obj) => { Console.WriteLine("Hello Alien! I am from {0}", obj); }, "Task 4"); //Use Lambda Expression and an anonymous method + Setting Task State |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int number = 10; | |
//Using task factory + Setting Task State + Returning Result | |
Task<int> T1 = Task.Factory.StartNew<int>(obj => | |
{ | |
int num = (int)obj; | |
return num * num; | |
}, number); | |
Console.WriteLine("T1 Task Result" + T1.Result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a simple task using taskfactory | |
Task.Factory.StartNew(() => { Console.WriteLine("Simple Task of Factory"); }); | |
//Create a task using factory + Setting Task State + Returning Result | |
Task<int> T1 = Task.Factory.StartNew<int>(obj => | |
{ | |
int a = (int)obj; | |
return a * a; | |
}, 10); | |
Console.WriteLine("(Not)Simple Task of Factory" + T1.Result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CancellationTokenSource cancelSource = new CancellationTokenSource(); | |
CancellationToken cancelToken = cancelSource.Token; | |
//1 Monitoring Cancellation with polling | |
Task T1 = new Task(() => | |
{ | |
for (int i = 0; i < int.MaxValue; i++) | |
{ | |
if (token.IsCancellationRequested) { | |
// tidy up and release resources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CancellationTokenSource source1 = new CancellationTokenSource(); | |
CancellationTokenSource source2 = new CancellationTokenSource(); | |
CancellationTokenSource source3 = new CancellationTokenSource(); | |
//Composite Cancellation Source example | |
CancellationTokenSource compositeSource = CancellationTokenSource.CreateLinkedTokenSource(source1.Token, source2.Token, source3.Token); | |
//Create 3 tasks and pass token sources to different tasks and call each Start methods | |
//Monitor multiple task cancellation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CancellationTokenSource tokenSource = new CancellationTokenSource(); | |
CancellationToken token = tokenSource.Token; | |
Task T1 = new Task(() => { Console.WriteLine("Task 1"); }, token); | |
Task T2 = new Task(() => { Console.WriteLine("Task 2"); }, token); | |
T1.Start(); | |
T2.Start(); | |
//Number of ways to achieve wait |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task T1 = new Task(() => { throw new ArgumentOutOfRangeException() { Source = "T1"}; }); | |
Task T2 = new Task(() => { throw new NullReferenceException(); }); | |
Task T3 = new Task(() => { Console.WriteLine("Artificial Cancellation"); throw new OperationCanceledException(); }); | |
T1.Start(); T2.Start(); T3.Start(); | |
try{ | |
Task.WaitAll(T1, T2, T3); | |
} | |
catch (AggregateException ex){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CancellationTokenSource source = new CancellationTokenSource(); | |
CancellationToken token = source.Token; | |
Task T1 = new Task(() => { | |
for(i=0;i<10000;i++){ | |
token.ThrowIfCancellationRequested(); | |
} | |
}, token); | |
Task T2 = new Task(() => { throw new IndexOutOfRangeException(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lazy<Task<string>> lazyTask = new Lazy<Task<string>>(() => | |
Task<string>.Factory.StartNew(() => | |
{ | |
Console.WriteLine("Task Body Working ....."); | |
return "TaskBody Result"; | |
}) | |
); | |
Console.WriteLine("Accessing lazy Task Result {0}", lazyTask.Value.Result); |
OlderNewer