Synchronous and Asynchronous methodology in C# Part:- 4

23
www.learncsharptutoria l.com Synchronous and Asynchronous methodology in C# In article we will discuss about concepts of Asynchronous and Synchronous methodology in C# programming eating new project For ease of learning practically about above mentioned methodology, we will create a console application using C# language as new project within Visual Studio 2015 in this case name of the new project created is kept default “ConsoleApplication2” as given by Visual Studio. Please Note creation of new project steps are going to be same as we had mentioned in article of this series. ating new class library Next, we will add a new Class library with that a new DLL file gets created which will be referred within “ConsoleApplication2”. To create class library do right click on the Solution and click on Add > New Project.

Transcript of Synchronous and Asynchronous methodology in C# Part:- 4

Page 1: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

In article we will discuss about concepts of Asynchronous and Synchronous methodology in C# programming

Creating new projectFor ease of learning practically about above mentioned methodology, we will create a console application using C# language as new project within Visual Studio 2015 in this case name of the new project created is kept default “ConsoleApplication2” as given by Visual Studio.

Please Note creation of new project steps are going to be same as we had mentioned in our previous Part 1 article of this series.

Creating new class libraryNext, we will add a new Class library with that a new DLL file gets created which will be referred within “ConsoleApplication2”.To create class library do right click on the Solution and click on Add > New Project.

Page 2: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Page 3: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# With that a New Project window will be opened on the left side Visual C# under that select Windows and then select Class Library give name “FactorialCal” to this project.

Page 4: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# Once the Class Library project is added it will be displayed under the Solution below to ConsoleApplication2 project.

Page 5: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# On Class.cs write the following Factorial program which will achieve product(multiply it) of given number by user display result of it product. Like if we would like to calculate Factorial of numeric 5 is then thisFactorial program will calculate it and display its result as 5 = 5x4x3x2x1 = 120.

For that under the Factorial program we have “FactorialCal” named class followed within it has method which uses FOR loop for the same in order to achieve result.

The method given “Calculate” is the one who takes in number given by the user and does the Factorial calculation over it and gives out the result.

Number given by the user is stored in the result variable which has “int” as data type. Then with the help of “FOR loop” it will loop starting from value ‘1’ then incrementing it one by one until the desired number given by user is achieved. So for every loop result is getting multiplied by the earlier result value of “FOR loop”.

for (i = 1; i< number, i++){result = result * i;}

Finally it will return the result value with the help of following code line

return result;

Page 6: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Creating new projectNow in order to use the above Factorial program which is written and available under Class Library in console application we need to do reference. For that on Solution Explorer simply doing right click on the “References” and clicking “Add Reference…” under “ConsoleApplication2” as shown in the image down below.

Page 7: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

After clicking on Add Reference it will open Reference Manager screen. On the left side under this window click on Projects > Solution. Then on Solution it will show the “FactorialCal” class library just select it by doing a tick on the checkbox. And finally do a click on OK to close the Reference window.

Page 8: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Page 9: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# Now if you see under “ConsoleApplication2” project after expanding “References” newly added “FactorialCal” class library can be seen as shown in the image down below.

Page 10: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Synchronous MethodologySynchronous Calling method or function

Now go to Program.cs of ConsoleApplication2 and do the following: -

1. As we want to the FactorialCal class write statement “using FactorialCalLab” which states importing of the namespace which is currently class library. Once the namespace is imported we can easily create object and give input to do get Factorial value of the input given.

2. Create object the FactorialCal class by writing following line in order to use under console application.

FactorialLabobj = new FactorialLab();

3. Write a line which will display on the console prompt “Calculation Started”

Console.WriteLine(“Calculation Started”);

Page 11: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# 4. Create variable called “result” with datatype as “int” and mention its object ‘obj’ with the method name and pass the input value, here given is 25. Frame code statement as below: -

int result = obj.Calculate(25);

5. Next line written is to display Factorial output of the given value

Console.WriteLine(“result”);

6. After the calculation is finished it will display the following statement on console prompt

Console.WriteLine(“Calculation Finished”);

7. And finally a display statement which will be displayed on the console prompt

Console.WriteLine(“Program Completed”);

Page 12: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Now execute the program by pressing ctrl+F5 on keyboard and you can see the result has been executed successfully and displayed all the content. But here we are not able to make out the synchronous sequence as the value which we have entered is small. It gets executed very fast as soon as the console prompt is open and hardly difference is getting noticed.

So we will enter some big value to get Factorial result which will take time to process and get to see synchronous sequence.

Page 13: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

We will use “BigInteger” datatype instead of “int” which can accommodate huge number within. For that we have to first add reference of System.Numerics from the Framework library on both Console Application as well Class Library “FactorialCal” as shown it the image down below.

Synchronous call with big value

Page 14: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Page 15: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# Secondly we have import namespace of Numerics which belong to System class on both “Class.cs” of “FactorialCal” class library and “Program.cs” of “ConsoleApplication2”. In order to import namespace write this syntax “using System.Numerics;” at the top.And then change “int” datatype to “BigInteger” wherever mentioned in both Console Application as well Class Library “FactorialCal”. On Console Application give input value whose factorial value need to be found.

Page 16: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C# Again execute the program and check its execution. After pressing ctrl+F5 its console prompt will open and gets started execution with first line “Calculation Started” and then factorial calculation will start since input value is big it will take time to complete it till then other lines will also not execute “Calculation Finished” and “Program Completed”.

This way of executing the program is termed as Synchronous programming where “caller” has to wait until “callee” send it response back and then “caller” completes it remaining execution.

Page 17: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

In Asynchronous methodology “caller” does not have to wait until the “callee” has send its response. Caller can complete its execution and then later completecallee’s .

Asynchronous methodology -Threading

Page 18: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

In order to achieve this logically C# has practical implementation called Threading where each process will use one thread to accomplish it and run simultaneously to other thread to achieve parallel execution or in order words they will execute asynchronously.

Next we will see introduction of Threading into our existing source code.

1. For first import namespace of threading on “Program.cs” by writing following line code

Page 19: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Now in order to make process as asynchronous introduce thread by creating object of the threading class whose namespace was imported using previous line code.After displaying statement of “Calculation Started” create an object of thread class likewise which we did earlier. With this statement we will also pass the value to calculate its factorial within the parameter along with lambda expression denoted by “=>”. To know more on lambda expression get reference here

usingSystem.Threading;

Thread t1 = new Thread(() =>obj.Calculate(1000000));

Invoke the thread object “t1” to start its threading execution. Please note with this threading execution it will only do the Factorial calculation.

t1.Start();

While the other thread running is itself Main method which will display the “Calculation Started” statement.So parallel execution will start with Main method running as first thread and then on other thread “t1” it will be running the Factorial calculation.

Page 20: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Now go and build the solution by clicking on the Rebuild option given on the top. After the solution is successfully build press combination key of ctrl+F5 on keyboard to display result on the console prompt and to know the whether process is actually working asynchronously as decided.

Page 21: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

From the below image result first line under the Main method running on first thread is displaying result “Calculation Started” and other thread “t1” is running calculation of the factorial program output has not arrived but still next line from Main method running on first thread which contains “Calculation Finished” and final line of “Program Completed” has executed and displayed on the console screen. This clearly states that each process is running on different thread and the caller has not to wait for callee any more, in short asynchronous I achieved.

Page 22: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Synchronous and Asynchronous methodology in C#

Page 23: Synchronous and Asynchronous methodology in C# Part:- 4

www.learncsharptutorial.com

Still this asynchronous program has problem that caller had not been provided with the output of callee’s running process. This can be achieved by working with Delegates and events.

In the next article we will learn about the same in more details with concept and practical.

With this lab we also have following learning video for you from our fresher’s Learn C# is 100 hrs series: -

Synchronous and Asynchronous methodology in C#