Skip to content

Instantly share code, notes, and snippets.

View aliabidzaidi's full-sized avatar
🌱

Ali Abid Zaidi aliabidzaidi

🌱
View GitHub Profile
@aliabidzaidi
aliabidzaidi / DifferentWaysToCreateTask.cs
Last active May 31, 2019 18:15
Different ways to create tasks in .net
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();
@aliabidzaidi
aliabidzaidi / SettingTaskStates.cs
Created May 31, 2019 18:05
Different ways to create a task and setting their task states
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
@aliabidzaidi
aliabidzaidi / ReturnResultFromTask.cs
Last active June 2, 2019 17:15
Returning result from a task and passing state
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);
@aliabidzaidi
aliabidzaidi / UsingTaskFactory.cs
Last active June 1, 2019 07:09
Creating task, returning result and passing state with TaskFactory
//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);
@aliabidzaidi
aliabidzaidi / WaysToMonitorCancelTasks.cs
Last active June 1, 2019 08:56
Different ways to Monitor a Canceled task in TPL
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
@aliabidzaidi
aliabidzaidi / CompositeCancellation.cs
Created June 1, 2019 09:28
Cancelling multiple tasks together by using a composite cancellation source
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
@aliabidzaidi
aliabidzaidi / WaitingForTasks.cs
Created June 1, 2019 10:16
Different ways to wait for tasks
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
@aliabidzaidi
aliabidzaidi / HandlingExceptions.cs
Last active June 3, 2019 07:40
A simple way to handle Exceptions thrown by tasks
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){
@aliabidzaidi
aliabidzaidi / HandlingSpecificException.cs
Last active June 3, 2019 06:51
Handling Custome Exceptions like operation Cancelled Exceptions
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(); });
@aliabidzaidi
aliabidzaidi / LazyTask.cs
Created June 1, 2019 19:07
Creating & Executing Tasks Lazily
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);