Skip to content

Commit 435af70

Browse files
CopilotBillWagnergewarren
authored
Restructure VB.NET async article to follow C# breakfast tutorial format with proper VB syntax (#48186)
* Initial plan * Update VB async article structure and add VB code snippets Co-authored-by: BillWagner <[email protected]> * Restructure VB async article to follow C# breakfast tutorial format Co-authored-by: BillWagner <[email protected]> * Update docs/visual-basic/programming-guide/concepts/async/index.md * Update docs/visual-basic/programming-guide/concepts/async/index.md * Address review feedback: fix relative link, convert to ::: code format, add C# diagrams Co-authored-by: BillWagner <[email protected]> * Update docs/visual-basic/programming-guide/concepts/async/snippets/breakfast/breakfast.vbproj * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 4c0c57f commit 435af70

File tree

8 files changed

+549
-159
lines changed

8 files changed

+549
-159
lines changed

docs/visual-basic/programming-guide/concepts/async/index.md

Lines changed: 267 additions & 159 deletions
Large diffs are not rendered by default.
48.8 KB
Loading
19.6 KB
Loading
20.9 KB
Loading
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Imports System
2+
Imports System.Collections.Generic
3+
Imports System.Threading.Tasks
4+
5+
' <AsyncBreakfast>
6+
Module AsyncBreakfastProgram
7+
Async Function Main() As Task
8+
Dim cup As Coffee = PourCoffee()
9+
Console.WriteLine("coffee is ready")
10+
11+
Dim eggs As Egg = Await FryEggsAsync(2)
12+
Console.WriteLine("eggs are ready")
13+
14+
Dim hashBrown As HashBrown = Await FryHashBrownsAsync(3)
15+
Console.WriteLine("hash browns are ready")
16+
17+
Dim toast As Toast = Await ToastBreadAsync(2)
18+
ApplyButter(toast)
19+
ApplyJam(toast)
20+
Console.WriteLine("toast is ready")
21+
22+
Dim oj As Juice = PourOJ()
23+
Console.WriteLine("oj is ready")
24+
Console.WriteLine("Breakfast is ready!")
25+
End Function
26+
27+
Private Async Function ToastBreadAsync(slices As Integer) As Task(Of Toast)
28+
For slice As Integer = 0 To slices - 1
29+
Console.WriteLine("Putting a slice of bread in the toaster")
30+
Next
31+
Console.WriteLine("Start toasting...")
32+
Await Task.Delay(3000)
33+
Console.WriteLine("Remove toast from toaster")
34+
35+
Return New Toast()
36+
End Function
37+
38+
Private Async Function FryHashBrownsAsync(patties As Integer) As Task(Of HashBrown)
39+
Console.WriteLine($"putting {patties} hash brown patties in the pan")
40+
Console.WriteLine("cooking first side of hash browns...")
41+
Await Task.Delay(3000)
42+
For patty As Integer = 0 To patties - 1
43+
Console.WriteLine("flipping a hash brown patty")
44+
Next
45+
Console.WriteLine("cooking the second side of hash browns...")
46+
Await Task.Delay(3000)
47+
Console.WriteLine("Put hash browns on plate")
48+
49+
Return New HashBrown()
50+
End Function
51+
52+
Private Async Function FryEggsAsync(howMany As Integer) As Task(Of Egg)
53+
Console.WriteLine("Warming the egg pan...")
54+
Await Task.Delay(3000)
55+
Console.WriteLine($"cracking {howMany} eggs")
56+
Console.WriteLine("cooking the eggs ...")
57+
Await Task.Delay(3000)
58+
Console.WriteLine("Put eggs on plate")
59+
60+
Return New Egg()
61+
End Function
62+
63+
Private Function PourCoffee() As Coffee
64+
Console.WriteLine("Pouring coffee")
65+
Return New Coffee()
66+
End Function
67+
68+
Private Function PourOJ() As Juice
69+
Console.WriteLine("Pouring orange juice")
70+
Return New Juice()
71+
End Function
72+
73+
Private Sub ApplyJam(toast As Toast)
74+
Console.WriteLine("Putting jam on the toast")
75+
End Sub
76+
77+
Private Sub ApplyButter(toast As Toast)
78+
Console.WriteLine("Putting butter on the toast")
79+
End Sub
80+
End Module
81+
' </AsyncBreakfast>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Imports System
2+
Imports System.Collections.Generic
3+
Imports System.Threading.Tasks
4+
5+
' <ConcurrentBreakfast>
6+
Module ConcurrentBreakfastProgram
7+
Async Function Main() As Task
8+
Dim cup As Coffee = PourCoffee()
9+
Console.WriteLine("Coffee is ready")
10+
11+
Dim eggsTask As Task(Of Egg) = FryEggsAsync(2)
12+
Dim hashBrownTask As Task(Of HashBrown) = FryHashBrownsAsync(3)
13+
Dim toastTask As Task(Of Toast) = MakeToastWithButterAndJamAsync(2)
14+
15+
Dim breakfastTasks As New List(Of Task) From {eggsTask, hashBrownTask, toastTask}
16+
While breakfastTasks.Count > 0
17+
Dim finishedTask As Task = Await Task.WhenAny(breakfastTasks)
18+
If finishedTask Is eggsTask Then
19+
Console.WriteLine("eggs are ready")
20+
ElseIf finishedTask Is hashBrownTask Then
21+
Console.WriteLine("hash browns are ready")
22+
ElseIf finishedTask Is toastTask Then
23+
Console.WriteLine("toast is ready")
24+
End If
25+
Await finishedTask
26+
breakfastTasks.Remove(finishedTask)
27+
End While
28+
29+
Dim oj As Juice = PourOJ()
30+
Console.WriteLine("oj is ready")
31+
Console.WriteLine("Breakfast is ready!")
32+
End Function
33+
34+
Async Function MakeToastWithButterAndJamAsync(number As Integer) As Task(Of Toast)
35+
Dim toast As Toast = Await ToastBreadAsync(number)
36+
ApplyButter(toast)
37+
ApplyJam(toast)
38+
39+
Return toast
40+
End Function
41+
42+
Private Async Function ToastBreadAsync(slices As Integer) As Task(Of Toast)
43+
For slice As Integer = 0 To slices - 1
44+
Console.WriteLine("Putting a slice of bread in the toaster")
45+
Next
46+
Console.WriteLine("Start toasting...")
47+
Await Task.Delay(3000)
48+
Console.WriteLine("Remove toast from toaster")
49+
50+
Return New Toast()
51+
End Function
52+
53+
Private Async Function FryHashBrownsAsync(patties As Integer) As Task(Of HashBrown)
54+
Console.WriteLine($"putting {patties} hash brown patties in the pan")
55+
Console.WriteLine("cooking first side of hash browns...")
56+
Await Task.Delay(3000)
57+
For patty As Integer = 0 To patties - 1
58+
Console.WriteLine("flipping a hash brown patty")
59+
Next
60+
Console.WriteLine("cooking the second side of hash browns...")
61+
Await Task.Delay(3000)
62+
Console.WriteLine("Put hash browns on plate")
63+
64+
Return New HashBrown()
65+
End Function
66+
67+
Private Async Function FryEggsAsync(howMany As Integer) As Task(Of Egg)
68+
Console.WriteLine("Warming the egg pan...")
69+
Await Task.Delay(3000)
70+
Console.WriteLine($"cracking {howMany} eggs")
71+
Console.WriteLine("cooking the eggs ...")
72+
Await Task.Delay(3000)
73+
Console.WriteLine("Put eggs on plate")
74+
75+
Return New Egg()
76+
End Function
77+
78+
Private Function PourCoffee() As Coffee
79+
Console.WriteLine("Pouring coffee")
80+
Return New Coffee()
81+
End Function
82+
83+
Private Function PourOJ() As Juice
84+
Console.WriteLine("Pouring orange juice")
85+
Return New Juice()
86+
End Function
87+
88+
Private Sub ApplyJam(toast As Toast)
89+
Console.WriteLine("Putting jam on the toast")
90+
End Sub
91+
92+
Private Sub ApplyButter(toast As Toast)
93+
Console.WriteLine("Putting butter on the toast")
94+
End Sub
95+
End Module
96+
' </ConcurrentBreakfast>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Imports System
2+
Imports System.Collections.Generic
3+
Imports System.Threading.Tasks
4+
5+
' These classes are intentionally empty for the purpose of this example. They are simply marker classes for the purpose of demonstration, contain no properties, and serve no other purpose.
6+
Friend Class HashBrown
7+
End Class
8+
9+
Friend Class Coffee
10+
End Class
11+
12+
Friend Class Egg
13+
End Class
14+
15+
Friend Class Juice
16+
End Class
17+
18+
Friend Class Toast
19+
End Class
20+
21+
Module Program
22+
' <SynchronousBreakfast>
23+
Sub Main()
24+
Dim cup As Coffee = PourCoffee()
25+
Console.WriteLine("coffee is ready")
26+
27+
Dim eggs As Egg = FryEggs(2)
28+
Console.WriteLine("eggs are ready")
29+
30+
Dim hashBrown As HashBrown = FryHashBrowns(3)
31+
Console.WriteLine("hash browns are ready")
32+
33+
Dim toast As Toast = ToastBread(2)
34+
ApplyButter(toast)
35+
ApplyJam(toast)
36+
Console.WriteLine("toast is ready")
37+
38+
Dim oj As Juice = PourOJ()
39+
Console.WriteLine("oj is ready")
40+
Console.WriteLine("Breakfast is ready!")
41+
End Sub
42+
43+
Private Function PourOJ() As Juice
44+
Console.WriteLine("Pouring orange juice")
45+
Return New Juice()
46+
End Function
47+
48+
Private Sub ApplyJam(toast As Toast)
49+
Console.WriteLine("Putting jam on the toast")
50+
End Sub
51+
52+
Private Sub ApplyButter(toast As Toast)
53+
Console.WriteLine("Putting butter on the toast")
54+
End Sub
55+
56+
Private Function ToastBread(slices As Integer) As Toast
57+
For slice As Integer = 0 To slices - 1
58+
Console.WriteLine("Putting a slice of bread in the toaster")
59+
Next
60+
Console.WriteLine("Start toasting...")
61+
Task.Delay(3000).Wait()
62+
Console.WriteLine("Remove toast from toaster")
63+
64+
Return New Toast()
65+
End Function
66+
67+
Private Function FryHashBrowns(patties As Integer) As HashBrown
68+
Console.WriteLine($"putting {patties} hash brown patties in the pan")
69+
Console.WriteLine("cooking first side of hash browns...")
70+
Task.Delay(3000).Wait()
71+
For patty As Integer = 0 To patties - 1
72+
Console.WriteLine("flipping a hash brown patty")
73+
Next
74+
Console.WriteLine("cooking the second side of hash browns...")
75+
Task.Delay(3000).Wait()
76+
Console.WriteLine("Put hash browns on plate")
77+
78+
Return New HashBrown()
79+
End Function
80+
81+
Private Function FryEggs(howMany As Integer) As Egg
82+
Console.WriteLine("Warming the egg pan...")
83+
Task.Delay(3000).Wait()
84+
Console.WriteLine($"cracking {howMany} eggs")
85+
Console.WriteLine("cooking the eggs ...")
86+
Task.Delay(3000).Wait()
87+
Console.WriteLine("Put eggs on plate")
88+
89+
Return New Egg()
90+
End Function
91+
92+
Private Function PourCoffee() As Coffee
93+
Console.WriteLine("Pouring coffee")
94+
Return New Coffee()
95+
End Function
96+
' </SynchronousBreakfast>
97+
End Module
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)