ASYNCHRONOUS PROGRAMMING
IN .NET
1. Introduction
Asynchronous Programming in .NET
• From basic concepts to practical async/await usage
2. Synchronous Program Execution
• Code executes line by line, in order
• Thread MUST WAIT for function to complete
• Like queuing: person in front finishes, next person proceeds
• When to use:
- Simple code, no optimization needed
- Fast CPU, waiting not an issue
- Easy to understand and debug
• Problem: Thread gets blocked when waiting for I/O
→ Thread does nothing → waste of resources
3. Asynchronous Program Execution
• Code does NOT NEED TO WAIT for other functions
• Thread RETURNS IMMEDIATELY, continues with other work
• When async function completes → calls callback
• Benefits:
- Increase performance: Thread not blocked
- Increase responsiveness: UI does not freeze
- Better resource utilization: CPU does not wait for I/O
• Patterns in .NET:
1. APM: [Link] - NO LONGER SUPPORTED in .NET Core
2. EAP: Uses events
3. TAP: Task and async/await - MOST MODERN APPROACH
• Does async create a new thread?
- I/O-bound: NO new thread, just waits for I/O
- CPU-bound: MAY create a new thread
• Async vs Parallel:
- Parallel: Divides work, runs in parallel on multiple CPUs/cores
- Async: Does not block thread, does other work while waiting for I/O
4. Demo 01: EAP Pattern
Demo Event-Based Asynchronous Pattern (EAP) download web page
• EAP uses events to handle async results
• Example: [Link] event
• NOTE: EAP is old pattern, should use TAP
5. When to Use Asynchronous Programming
Use Async when:
1. I/O Operations:
- Reading/writing files
- Calling APIs over network
- Querying database
- Sending email
2. Reason:
- CPU does nothing while waiting for I/O → waste
- Async allows thread to do other work
• Should we use async for everything?
NO! Only use when:
- There are I/O operations
- Need to increase responsiveness (UI app)
- Multiple independent tasks run in parallel
• Does async make code run faster?
NOT DIRECTLY! But:
- Increases throughput (number of tasks processed)
- Increases responsiveness (UI does not freeze)
- Better resource utilization
6. Introducing async and await
• What are async and await?
- async: Marks function as asynchronous method
- await: WAITS for async task, but does NOT BLOCK thread
• Execution flow:
1. Call async function → returns Task immediately
2. Thread continues with other work
3. Encounter await → if task not done, thread released
4. Task completes → continues code after await
• Important points:
- async method returns Task, Task<T>, or void (not recommended)
- await can only be used in async method
- await does NOT BLOCK thread
• Does await block the thread?
NO! Thread released to do other work
→ Major difference from synchronous code
• Why must we have async when using await?
Compiler needs to know to:
- Automatically wrap result in Task
- Create state machine to manage continuation
7. Demo 02: TAP Pattern
Demo Task-Based Asynchronous Pattern (TAP) with async/await
• MOST MODERN and RECOMMENDED approach
• Uses Task and async/await instead of events
• Code easier to read and maintain than EAP
• TAP advantages:
- Code easier to read (similar to synchronous)
- Easier error handling (try-catch)
- Easier to combine multiple async operations
- Supports cancellation
8. Demo 03: WPF Application with HttpClient
Demo WPF app download multiple websites simultaneously
• Why use async?
- Downloading from network is I/O operation
- Synchronous → UI FREEZES
- Async → UI RESPONSIVE
• Downloading multiple websites:
- Use [Link] to wait for ALL tasks
- Download in parallel
• Why does UI not freeze?
await does NOT BLOCK UI thread
UI thread released to handle events
9. Summary
Summary:
1. Synchronous vs Asynchronous:
- Synchronous: Executes sequentially, waits for each step
- Asynchronous: Does not block thread, utilizes waiting time
2. When to use Async:
- I/O operations (file, network, database)
- Need to increase responsiveness (UI apps)
- Multiple independent tasks
3. async/await:
- async: Marks async method
- await: Waits for task but does not block thread
- Modern pattern: TAP (Task-Based Asynchronous Pattern)
4. Benefits:
- Increases throughput
- Increases responsiveness
- Better resource utilization
• Remember: Async makes code MORE EFFICIENT!