Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
426 changes: 267 additions & 159 deletions docs/visual-basic/programming-guide/concepts/async/index.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks

' <AsyncBreakfast>
Module AsyncBreakfastProgram
Async Function Main() As Task
Dim cup As Coffee = PourCoffee()
Console.WriteLine("coffee is ready")

Dim eggs As Egg = Await FryEggsAsync(2)
Console.WriteLine("eggs are ready")

Dim hashBrown As HashBrown = Await FryHashBrownsAsync(3)
Console.WriteLine("hash browns are ready")

Dim toast As Toast = Await ToastBreadAsync(2)
ApplyButter(toast)
ApplyJam(toast)
Console.WriteLine("toast is ready")

Dim oj As Juice = PourOJ()
Console.WriteLine("oj is ready")
Console.WriteLine("Breakfast is ready!")
End Function

Private Async Function ToastBreadAsync(slices As Integer) As Task(Of Toast)
For slice As Integer = 0 To slices - 1
Console.WriteLine("Putting a slice of bread in the toaster")
Next
Console.WriteLine("Start toasting...")
Await Task.Delay(3000)
Console.WriteLine("Remove toast from toaster")

Return New Toast()
End Function

Private Async Function FryHashBrownsAsync(patties As Integer) As Task(Of HashBrown)
Console.WriteLine($"putting {patties} hash brown patties in the pan")
Console.WriteLine("cooking first side of hash browns...")
Await Task.Delay(3000)
For patty As Integer = 0 To patties - 1
Console.WriteLine("flipping a hash brown patty")
Next
Console.WriteLine("cooking the second side of hash browns...")
Await Task.Delay(3000)
Console.WriteLine("Put hash browns on plate")

Return New HashBrown()
End Function

Private Async Function FryEggsAsync(howMany As Integer) As Task(Of Egg)
Console.WriteLine("Warming the egg pan...")
Await Task.Delay(3000)
Console.WriteLine($"cracking {howMany} eggs")
Console.WriteLine("cooking the eggs ...")
Await Task.Delay(3000)
Console.WriteLine("Put eggs on plate")

Return New Egg()
End Function

Private Function PourCoffee() As Coffee
Console.WriteLine("Pouring coffee")
Return New Coffee()
End Function

Private Function PourOJ() As Juice
Console.WriteLine("Pouring orange juice")
Return New Juice()
End Function

Private Sub ApplyJam(toast As Toast)
Console.WriteLine("Putting jam on the toast")
End Sub

Private Sub ApplyButter(toast As Toast)
Console.WriteLine("Putting butter on the toast")
End Sub
End Module
' </AsyncBreakfast>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks

' <ConcurrentBreakfast>
Module ConcurrentBreakfastProgram
Async Function Main() As Task
Dim cup As Coffee = PourCoffee()
Console.WriteLine("Coffee is ready")

Dim eggsTask As Task(Of Egg) = FryEggsAsync(2)
Dim hashBrownTask As Task(Of HashBrown) = FryHashBrownsAsync(3)
Dim toastTask As Task(Of Toast) = MakeToastWithButterAndJamAsync(2)

Dim breakfastTasks As New List(Of Task) From {eggsTask, hashBrownTask, toastTask}
While breakfastTasks.Count > 0
Dim finishedTask As Task = Await Task.WhenAny(breakfastTasks)
If finishedTask Is eggsTask Then
Console.WriteLine("eggs are ready")
ElseIf finishedTask Is hashBrownTask Then
Console.WriteLine("hash browns are ready")
ElseIf finishedTask Is toastTask Then
Console.WriteLine("toast is ready")
End If
Await finishedTask
breakfastTasks.Remove(finishedTask)
End While

Dim oj As Juice = PourOJ()
Console.WriteLine("oj is ready")
Console.WriteLine("Breakfast is ready!")
End Function

Async Function MakeToastWithButterAndJamAsync(number As Integer) As Task(Of Toast)
Dim toast As Toast = Await ToastBreadAsync(number)
ApplyButter(toast)
ApplyJam(toast)

Return toast
End Function

Private Async Function ToastBreadAsync(slices As Integer) As Task(Of Toast)
For slice As Integer = 0 To slices - 1
Console.WriteLine("Putting a slice of bread in the toaster")
Next
Console.WriteLine("Start toasting...")
Await Task.Delay(3000)
Console.WriteLine("Remove toast from toaster")

Return New Toast()
End Function

Private Async Function FryHashBrownsAsync(patties As Integer) As Task(Of HashBrown)
Console.WriteLine($"putting {patties} hash brown patties in the pan")
Console.WriteLine("cooking first side of hash browns...")
Await Task.Delay(3000)
For patty As Integer = 0 To patties - 1
Console.WriteLine("flipping a hash brown patty")
Next
Console.WriteLine("cooking the second side of hash browns...")
Await Task.Delay(3000)
Console.WriteLine("Put hash browns on plate")

Return New HashBrown()
End Function

Private Async Function FryEggsAsync(howMany As Integer) As Task(Of Egg)
Console.WriteLine("Warming the egg pan...")
Await Task.Delay(3000)
Console.WriteLine($"cracking {howMany} eggs")
Console.WriteLine("cooking the eggs ...")
Await Task.Delay(3000)
Console.WriteLine("Put eggs on plate")

Return New Egg()
End Function

Private Function PourCoffee() As Coffee
Console.WriteLine("Pouring coffee")
Return New Coffee()
End Function

Private Function PourOJ() As Juice
Console.WriteLine("Pouring orange juice")
Return New Juice()
End Function

Private Sub ApplyJam(toast As Toast)
Console.WriteLine("Putting jam on the toast")
End Sub

Private Sub ApplyButter(toast As Toast)
Console.WriteLine("Putting butter on the toast")
End Sub
End Module
' </ConcurrentBreakfast>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks

' 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.
Friend Class HashBrown
End Class

Friend Class Coffee
End Class

Friend Class Egg
End Class

Friend Class Juice
End Class

Friend Class Toast
End Class

Module Program
' <SynchronousBreakfast>
Sub Main()
Dim cup As Coffee = PourCoffee()
Console.WriteLine("coffee is ready")

Dim eggs As Egg = FryEggs(2)
Console.WriteLine("eggs are ready")

Dim hashBrown As HashBrown = FryHashBrowns(3)
Console.WriteLine("hash browns are ready")

Dim toast As Toast = ToastBread(2)
ApplyButter(toast)
ApplyJam(toast)
Console.WriteLine("toast is ready")

Dim oj As Juice = PourOJ()
Console.WriteLine("oj is ready")
Console.WriteLine("Breakfast is ready!")
End Sub

Private Function PourOJ() As Juice
Console.WriteLine("Pouring orange juice")
Return New Juice()
End Function

Private Sub ApplyJam(toast As Toast)
Console.WriteLine("Putting jam on the toast")
End Sub

Private Sub ApplyButter(toast As Toast)
Console.WriteLine("Putting butter on the toast")
End Sub

Private Function ToastBread(slices As Integer) As Toast
For slice As Integer = 0 To slices - 1
Console.WriteLine("Putting a slice of bread in the toaster")
Next
Console.WriteLine("Start toasting...")
Task.Delay(3000).Wait()
Console.WriteLine("Remove toast from toaster")

Return New Toast()
End Function

Private Function FryHashBrowns(patties As Integer) As HashBrown
Console.WriteLine($"putting {patties} hash brown patties in the pan")
Console.WriteLine("cooking first side of hash browns...")
Task.Delay(3000).Wait()
For patty As Integer = 0 To patties - 1
Console.WriteLine("flipping a hash brown patty")
Next
Console.WriteLine("cooking the second side of hash browns...")
Task.Delay(3000).Wait()
Console.WriteLine("Put hash browns on plate")

Return New HashBrown()
End Function

Private Function FryEggs(howMany As Integer) As Egg
Console.WriteLine("Warming the egg pan...")
Task.Delay(3000).Wait()
Console.WriteLine($"cracking {howMany} eggs")
Console.WriteLine("cooking the eggs ...")
Task.Delay(3000).Wait()
Console.WriteLine("Put eggs on plate")

Return New Egg()
End Function

Private Function PourCoffee() As Coffee
Console.WriteLine("Pouring coffee")
Return New Coffee()
End Function
' </SynchronousBreakfast>
End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>