Skip to main content
  1. Quick Notes & Explanations/

8. Synchronous and Asynchronous Concepts

·2 mins· loading · loading ·
Table of Contents

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

Related

7. What Are Zenject and VContainer Not?
·3 mins· loading · loading
All of the systems mentioned above are frameworks. While I’ve provided examples specific to Unity, we could also include native C# frameworks like AutoFac, but these are systems adapted and optimized specifically for Unity.
6. What is SOLID, and What is It Not?
·1 min· loading · loading
The following five principles, which we have previously explained, form our SOLID principles: Single Responsibility Principle Open/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle I won’t redefine these terms.
5. Choosing the Appropriate Architecture
·4 mins· loading · loading
After the reminders about optimization in the first three sections, we can bring together the principles from the fourth section to develop an architecture that can be applied to our project.
4. Design Pattern Examples
·10 mins· loading · loading
We can categorize Design Patterns under 3 main headings: Architectural Patterns Examples: MVC (Model-View-Controller), SOLID, MVVM… Design Patterns It is a specific Architectural Pattern, such as Singleton. Using it alone in a project is not sufficient, and optimization with several different patterns is necessary.
3. Why should we use architecture?
·3 mins· loading · loading
One of the fundamental advantages of working with principles and architectures in our projects is that it allows us to benefit from the advantages of libraries and plugins developed specifically for those principles.
2. GC Alloc ve GC Collect
·2 mins· loading · loading
In C# and some Object-Oriented Programming languages, there is the term Garbage Collector. However, in some mid-level programming languages, it is the programmer’s responsibility to remove objects that are no longer needed from memory.