Advanced Programming: BSCS 3rd
Describe threading and synchronization in C#. How do asynchronous
delegates and application domains contribute to efficient program
execution?
�Synchronous Programming in C# (Complete Detailed Notes)
�1. Definition & Concept
� What is Synchronous Programming?
Synchronous programming means each operation completes before the
next one starts.
It is blocking: the calling thread waits for the method to finish before
moving on.
Commonly used for CPU-bound tasks, short-lived operations, and simple
applications.
�2. Characteristics of Synchronous Code
Feature Explanation
Blocking Each function call blocks the thread until it returns.
Sequential Instructions execute in a strict top-to-bottom order.
Predictable Easier to debug due to simple control flow.
Less efficient Can waste resources during I/O-bound operations.
UI freezing In UI-based apps, long-running tasks block the UI and freeze the
risk interface.
�3. Where It Is Commonly Used
Console applications.
Background services with no user interaction.
Fast CPU-based computations.
Legacy systems that don’t support async/await.
�4. How It Works (Internally)
The current thread (e.g., main thread or worker thread) executes the
method.
The execution halts at blocking calls (e.g., file read, database fetch).
Thread remains occupied and can’t perform other tasks until the method
finishes.
�5. Common Synchronous Methods in .NET
Category Method Example Description
Reads all text from a file
File I/O File.ReadAllText(path)
synchronously
Network Downloads a string from a
WebClient.DownloadString(url)
I/O URL synchronously
Executes a query and reads
Database SqlCommand.ExecuteReader()
data synchronously
HTTP Gets HTTP response in
HttpWebRequest.GetResponse()
Client blocking way
�6. Step-by-Step: Creating Synchronous Functions in C#
�Step 1: Create a Method
7. Full Examples in Synchronous Style
�Example A: Reading a File Synchronously
�Example C: SQL Database (Synchronous)
�9. When to Use Synchronous Programming
✅Recommended If:
Operations are fast and lightweight.
Application does not need high responsiveness.
You are writing console applications or scripts.
The code is primarily CPU-bound and not I/O-bound.
�10. Best Practices for Writing Synchronous Code
Avoid blocking calls in UI applications (e.g., WinForms, WPF).
For I/O operations in real-time systems, prefer async alternatives.
Keep synchronous methods short and efficient.
Avoid mixing sync and async inappropriately (e.g., Task.Wait() or
Result on async methods).
�11. Advanced Tip: Thread.Sleep vs Blocking I/O
Thread.Sleep(5000) – Blocks the thread intentionally (CPU idle).
File.ReadAllText() – Blocks the thread during I/O (waiting for disk
read).
Both are blocking operations, but for different reasons.
�12. Transition from Synchronous to Asynchronous
Sync Method Async Equivalent
awaitFile.ReadAllTextAsync(pat
File.ReadAllText(path)
h)
WebClient.DownloadString awaithttpClient.GetStringAsync
() ()
ExecuteReader() await ExecuteReaderAsync()
Thread.Sleep() await Task.Delay()