-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharp2_8.html
More file actions
97 lines (86 loc) · 6.67 KB
/
Copy pathCSharp2_8.html
File metadata and controls
97 lines (86 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<article>
<h1>C# II: Page 8 - Modern Performance: Asynchronous Programming (Async/Await)</h1>
<section>
<h2>Beyond Sequential Code</h2>
<p>In the early days of programming, code ran in a straight line. If you needed to download a large file, the entire application would "Freeze" until the download was finished. This is because the "Main Thread" (the part of the computer that handles the UI) was blocked. Modern users expect applications to be snappy and responsive. <strong>Asynchronous Programming</strong> allows your code to start a long-running task (like a database query or an API call) and "Yield" the thread back to the system so it can do other work. When the task is finished, your code resumes exactly where it left off. In C#, this is handled elegantly by the <code>async</code> and <code>await</code> keywords.</p>
</section>
<section>
<h2>1. The <code>Task</code> and <code>Task<T></code> Objects</h2>
<p>In C#, an asynchronous operation is represented by a <strong>Task</strong>. Think of a Task as a "Promise" that some work will be completed in the future.</p>
<ul>
<li><strong>Task:</strong> Represents a task that returns no value (similar to <code>void</code>).</li>
<li><strong>Task<T>:</strong> Represents a task that will eventually return a value of type <code>T</code> (e.g., <code>Task<string></code>).</li>
</ul>
</section>
<section>
<h2>2. Using <code>async</code> and <code>await</code></h2>
<p>To make a method asynchronous, you mark it with the <code>async</code> modifier. Inside that method, you use <code>await</code> before any call that returns a Task. The <code>await</code> keyword tells the computer: "Pause this method here, free up the thread, and come back when this Task is done."</p>
<pre><code class="language-csharp">
public async Task<string> DownloadDataAsync(string url) {
using HttpClient client = new HttpClient();
// The UI stays responsive while this download happens!
string result = await client.GetStringAsync(url);
return result;
}
</code></pre>
</section>
<section>
<h2>3. Parallelism: Doing Many Things at Once</h2>
<p>Async is about <strong>Responsiveness</strong> (waiting without blocking). **Parallelism** is about <strong>Throughput</strong> (using multiple CPU cores to do work faster). C# provides the <code>Task.WhenAll</code> method to run multiple tasks simultaneously.</p>
<pre><code class="language-csharp">
var task1 = DoWorkAsync();
var task2 = DoOtherWorkAsync();
// Run BOTH at the same time and wait for both to finish
await Task.WhenAll(task1, task2);
</code></pre>
<div class="interactive-sim">
<h3>Async/Await Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cs2-8');
out.innerHTML = 'await DownloadDataAsync();<br>';
setTimeout(() => out.innerHTML += '>> Data downloaded without blocking!', 800);
">Run Simulation</button>
<div id="sim-output-cs2-8" class="sim-output"></div>
</div>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master the performance of modern C# with these three videos:</p>
<div style="display: flex; gap: 20px; flex-wrap: wrap; margin-top: 20px;">
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>1. Async/Await in C# Explained</strong><br>
<a href="https://www.youtube.com/watch?v=2ZPhxE90n9Y" target="_blank">Watch on YouTube →</a>
<p><small>The best visual guide to non-blocking code.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. Task vs Thread: What's the Difference?</strong><br>
<a href="https://www.youtube.com/watch?v=0_fGjS3u6fM" target="_blank">Watch on YouTube →</a>
<p><small>Understand the memory and performance trade-offs.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Parallel.ForEach and PLINQ</strong><br>
<a href="https://www.youtube.com/watch?v=f-Kov0XwY_8" target="_blank">Watch on YouTube →</a>
<p><small>Learn how to use every core of your CPU for massive data tasks.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The Waiter and the Coffee Machine</h2>
<p>Think of <strong>Synchronous</strong> programming like a **Chef at a Diner** who puts toast in the toaster and <em>stands there staring at it</em> until it pops up. He can't take orders or flip burgers while the toast is cooking. The whole kitchen stops. Think of <strong>Asynchronous (Async/Await)</strong> like a <strong>Professional Waiter</strong>. The waiter puts an order into the kitchen (Starts a Task). Instead of standing at the kitchen window (Blocking), the waiter goes and fills water glasses for other tables (Yielding the thread). When the food is ready (The Task completes), the waiter returns to the kitchen to pick up the plate and deliver it (Resuming the code). This is how a single person (or a single CPU thread) can handle 20 customers at once without anyone feeling ignored.</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/" target="_blank">Official Microsoft: Asynchronous Programming Guide</a></li>
<li><a href="https://www.baeldung.com/cs/async-await-csharp" target="_blank">Baeldung: Guide to Async/Await</a></li>
<li><a href="https://www.w3schools.com/cs/cs_async.php" target="_blank">W3Schools: C# Async Tutorial</a></li>
<li><a href="https://pythontutorial.net/advanced-python/python-asyncio/" target="_blank">Async Comparison: Python vs C#</a></li>
</ul>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="CSharp2_7.html" style="text-decoration: none; color: #6c757d;">← Previous: LINQ: Power Queries</a>
<a href="#" data-file="CSharp2_9.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Generics & Reflection →</a>
</div>
</footer>
</article>