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 async…await 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.
No comments:
Post a Comment