Thursday, 13 February 2014

Asynchronous vs. Synchronous Programming Execution in C#

 

 

Synchronous Programming

The difference between synchronous and asynchronous execution may seem a bit confusing at first. Program execution in most high-level languages is usually very straightforward. Your program starts at the first line of source code and each line of code executed sequentially thereafter. Easy enough.

 

 

 

image

 

 

// Exposure time in seconds

TakePicture(long lExposureTime)

Begin

//Do stuff to take a picture...

While (notCancelledByUser)

If (notCancelledByUser == TRUE)

Return NoError;

Else

Return Error;

End

 

Main

Begin

TakePicture(120);

Print(“TakePicture() function returns!”)

End

 

Synchronous program execution is somewhat similar to the above. Your program is executed line by line, one line at a time. Each time a function is called, program execution waits until that function returns before continuing to the next line of code.   This method of execution can have undesirable ramifications. Suppose a function is called to start a time consuming process. What if you want to stop the lengthy process? With synchronous execution, your program is “stuck,” waiting for the process to end, with no way out.

In the above pseudo-code, using synchronous execution, you’ll have to wait two minutes for the call to TakePicture() return and display the “TakePicture() function returns!” message. There is no way to cancel the picture.

 

Asynchronous Programming

Asynchronous execution avoids this bottleneck. You are essentially saying, “I know this function call is going to take a great deal of time, but my program thread doesn’t want to wait around while it executes.” Instead it will continue to run from the next line of code where it called this sub thread method.

 

image

 

 

 

Blogger Labels: Asynchronous,Synchronous,Execution,difference,Program,languages,Easy,Exposure,TakePicture,Begin,TRUE,Return,NoError,Else,Error,Main,Print,method,ramifications,Suppose,message,bottleneck,Instead,notCancelledByUser

No comments:

Post a Comment