Synchronous Programming
Most of the programs we write execute the code from top to bottom in the order it was written. Everything is done sequentially. For example, this code illustrates this:
var fs = GetFiles();
var data = fs.ReadFiles();
Debug.Log(“Program is running...”);
var lines = data.ToString().Split(“\\n”);
Debug.Log(lines.Length);
The code executes step by step in this order:
- Call the
GetFiles
method. - Retrieve the file content.
- Print “Program is running…” to the screen.
- Count the lines.
- Print the number of lines to the screen.
No operation can bypass another, and each operation must wait for the previous one to complete. It must execute sequentially and synchronously. This type of programming method is called Synchronous Programming.
Asynchronous Programming
Processing everything sequentially and having each operation wait for the previous one, as in synchronous programming, can slow down our program significantly, or even halt it until an operation is completed. For example, in the code above, the third line must wait for the previous line, i.e., the file reading process, to complete. If the file content is very large, these operations might take minutes. It’s not very wise to wait for the previous operation to finish just to print “Program is running…” on the screen. This is where asynchronous methods come into play.
Programming where the code flow is not sequential, operations do not wait for each other, and the code flow continues based on the state of operations is called Asynchronous Programming.
For more detailed examples, visit: Asynchronous Programming in C#
Reply by Email