Search This Blog

Saturday, 12 June 2021

Symphony with Asynchrony - C#


Let me explain asynchrony to you in a simple way.

Assume there are two functions named Main() and ICanDoAJob(). Main() is a function that executes line-by -line in a sequential manner. If there is a function such as LongRunning() which takes about 2 minutes to complete its execution, then Main() can cause the application to hang for 2min.

Hence Main() decides to give the job of executing LongRunning() to the function ICanDoAJob(), on a condition that ICanDoAJob() will take care of executing LongRunning() without disturbing or interrupting the work execution of Main().

ICanDoAJob(), now works independently, while Main() executes his calls without waiting for LongRunning() until it executes. This is known as asynchrony.


  

How is this Asynchrony achieved?

 

To achieve this asynchrony, let’s understand how a resource is executed.

The OS memory manager allocates memory for a resource to run on memory. When this is done, a Process Id is assigned, and each resource running in memory (RAM) is called as a process. Process is usually a background service or an executable (.exe). For example, when you open Visual Studio, a process is created for its underlying exe which is “devenv.exe”. This can be viewed in the Task Manager (of Windows OS), as shown in the screenshot (A) below. You will observe that it is allocated with a ProcessId. Similarly, Each process has atleast one thread known as the Main thread, that executes the process. In a normal program, this is the Main() method. A Thread can be considered as an independently executing task. If the Main() thread now spawns additional such threads which execute asynchronously, and merge with the Main() after completion, then technically this is known as asynchrony. The total threads for a particular process can be seen in the Task Manager -> Performance Tab -> Open Resource Monitor. Please observe the screenshot (B) to understand how many threads are created for the process “devenv.exe



 

(A)                                

 


                                

 

                                                                                                            (B)


                                      

In .Net, if one wants spawn threads from the Main() method, it can be achieved using a class known as the ThreadStart() delegate. This is highlighted in the code block below.

1.  Thread thr = new Thread(new ThreadStart(() => {
2.                                          for (int i = 0; i < 10; i++)
3.                                                         Console.WriteLine($"Thread {i}");
4.                                       }));
5.              thr.Start();
6.   

Here, the developer needs to take care of thread management efficiently through code. As threads run independently, this can be a problem if the thread is not stopped or merged correctly.

With the advent of new features introduced in .Net the concept of “Tasks” is introduced with a garnish of two best friends introduced in the form of keywords “async … await”. Async & await can be understood by the analogy of husband (async) and wife (await). The wife brings in order and creates a disciplined routine. Similarly, await will see that a task running asynchronously runs in an ordered manner. When async is used without await, the execution will be in a haphazard manner. The wife is always with the husband. Hence, await (wife) cannot be used if a method is not marked as async (husband).

Hence technically, there is a class in the System.Threading namespace, called Task.

The unit of code (An anonymous Lambda / a named function) to be executed asynchronously is created as a Task. To mark this unit of code to run asynchronously, the method is marked as “async”.

If there are multiple such tasks executing independently, where the output of one TaskA is an input to TaskB, then it is important to await the completion of the execution of TaskA, before passing its output to TaskB. Here it is important to use the keyword “await”.

Is it important to use await with async? Can async be used without await? The quick answer is YES.

But, unless you mark a method as async, await cannot be used. This means, to use await, a method must be marked as async. But if a method is marked as async, await may or may not be added.

The syntax for creating a Task is as follows

1.  Task.Run(()=>{});
2.   

In the above code, Run is a function, that takes Action delegate as a parameter. Let’s now create a function that uses asyncawait and Task. The following program will print the text on the console.

1.  static void Main(string[] args)
2.          {
3.             Method1();
4.          }
5.  public static void Method2()
6.          {
7.              for (int i = 0; i < 25; i++)
8.              {
9.                  Console.WriteLine($"M2-{i} Method 2");
10.               }
11.           }
12.   public static async void Method1()
13.           {
14.               await Task.Run(() =>
15.               {
16.                   for (int i = 0; i < 1000; i++)
17.                   {
18.                       Console.WriteLine($"M1-{i} Method 1");
19.                   }
20.               });
21.  

The same can be done using a TaskFactory, which creates tasks from a pool of tasks.

1.              TaskFactory factory = new TaskFactory();
2.              factory.StartNew(() => Method2());
3.              factory.StartNew(() => Method3(9999));
4.   
5.              Task.Factory.StartNew(() => Method2());
6.   

Tasks can also be pre-created and stored in a collection. In this case, it is better to use a thread-safe collection, such as ConcurrentBag<>. To do so, we create Task objects and add them to a ConcurrentBag, and then execute it wherever required. Have a look at the code below.

1.  public static async void UseConcurrentBag()
2.          {
3.   
4.              ConcurrentBag<Task> taskBag = new ConcurrentBag<Task>();
5.              Task t1 = new Task(() => Method1());
6.              Task t2 = new Task(() => Method2());
7.   
8.              taskBag.Add(t1);
9.              taskBag.Add(t2);
10.    
11.               foreach (var t in taskBag)
12.               {
13.                   t.Start();
14.                   Console.WriteLine($"Id: {t.Id}, Status: {t.Status}, IsCancellable: {t.IsCanceled}");
15.               }
16.           }
17.    

The difference between a thread and a Task is, a task can be cancelled midway. This is done by adding a CancellationToken at the time of creation of the task. This CancellationToken object is generated from a CancellationTokenSource class object. View the sample code below.

1.  public static void UsingTaskCancellation()
2.          {
3.              //STEP 1: Initialize Cancellation token source to cancel a task
4.              CancellationTokenSource tokenSource = new CancellationTokenSource();
5.              CancellationToken token = tokenSource.Token;
6.              
7.              //Start a new task by passing the cancellation token
8.              Task t = Task.Factory.StartNew(() => {
9.                  int counter = 1;
10.                   while (!token.IsCancellationRequested)
11.                   {
12.                       Console.WriteLine(counter++);
13.                       Thread.Sleep(1000);
14.                   }
15.               }, token);
16.    
17.               //On any key pressed by user, cancel the task
18.               Console.WriteLine("======== Press any key to cancel this task ========");
19.               Console.ReadKey();
20.               tokenSource.Cancel();   //Cancels a running task
21.               Console.WriteLine("Task Cancelled");
22.           }
23.    

 Hope you get a headstart to asynchrony and enjoy your programming journey ahead.

Monday, 24 May 2021

What would you when faced with the worst? - Poetry

The audio-video visual of my original composition "What would you do when faced with the worst?" as part of the channel "Pearls of Wisdom"



What do we do when faced with the worst?

A thought that usually never crosses our mind first.


In happier times, we enjoy or spend time whining

About things we lost inspite of working hard trying


To fulfill our goals or desires deep within

Not realising to have the gratitude for the moment we live in


What really happens when we are faced with the worst

Was never a formal topic discussed or conversed


This was usually for individual introspection

Considered a depressing, uninteresting self-conversation


Times have changed

We as world together faced


The disastrous pandemic ripping off lives

Situations Unpredictable, what more in store lies


As we see, but cannot help our family members in need

In hospitals, not knowing what condition in they might be


Whether quarantined at home or hospital

Individuals, families worrying about survival


Social distancing is the norm

Having friends, family, yet one alone faces the storm


Shortage of medications and resources

Media reports of deaths through its sources


All sorts of horrifying yet distressing thoughts one is now immersed

What would happen if the pandemic hits anyone of us first?

Then, yes, we would be faced with the worst.


Policies, rules are drawn out

Whether affected by the disease or not

Fear in its clutches, has us caught


We realize, everyone is alone

As one faces circumstances with its effects unknown


So it is, for every such problem of life

Whether a disease, trauma or strife


What to do, when you are un-able?

When the situation is uncontrollable?


The mantra is; solve it or accept it

By staying grounded with calm, while you face it.


But while in situation now

Can one think of a resolve, how?


When Winds of confusion, agitation diminish

The lights of Peace, Love, happiness we wish


Affirm positive notes, pass on healing vibes

To the mind which translates the belief it imbibes


Sending signals to our physical self

while improving aura healing oneself


Does this work, one may think

Positive belief is better than to accept to sink


Remember, Keep the light of Hope always lit

As even tough times will one day see the exit!


Cherish life with family, as you live today,

Thank God to have survived the day


To the worst you will not succumb,

But believe you will overcome

You are more than a survivor

A hero who will live to create a better tomorrow.



Creative Commons License
What would Happen if you are faced with the Worst? by Samprada N is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Based on a work at https://youtu.be/_C5mRIWP7Kg.

Monday, 3 May 2021

Tackle the Obs || Tacle - Motivation Booster Shots

Pearls of Wisdom is back with its Motivational Booster Shots for you! Today we talk about covid heroes from Mitti Cafe, who flourished in business while serving the society although they faced financial and physical crunches.

So here, we shall see a real example on "Tackle the Obs || Tacle"



Do watch this short motivation booster and share it with someone you know who needs it during these trying times.
If this video helped you, do share your story of motivation in comments and do consider subscribing to this channel.