Skip to content
Lesson 7 of 7

Step 1 of 5 · Reading · ~3 min

Learn

Modules, Generics, Async

VB.NET 11+ has full async/await — same model as C#.

Basic async

Imports System.Net.Http
Imports System.Threading.Tasks

Async Function FetchDataAsync(url As String) As Task(Of String)
    Using client = New HttpClient()
        Return Await client.GetStringAsync(url)
    End Using
End Function

Async Function MainAsync() As Task
    Dim data = Await FetchDataAsync("https://example.com")
    Console.WriteLine(data.Length)
End Function

Key keywords

  • Async — marks a method as asynchronous; allows Await inside
  • Await — wait for a Task without blocking the thread
  • Task / Task(Of T) — represents async work
  • Function ... As Task(Of T) — returns a value asynchronously
  • Sub ... As Task — returns nothing asynchronously (use Function ... As Task if possible)

Calling async from Main

Module Program
    Sub Main()
        MainAsync().Wait()        ' or .GetAwaiter().GetResult()
    End Sub

    Async Function MainAsync() As Task
        Dim result = Await ComputeAsync()
        Console.WriteLine(result)
    End Function
End Module

Or use Async Sub Main (.NET Core+):

Async Function Main() As Task
    ' ...
End Function

Parallel async

Dim t1 = FetchAsync("url1")
Dim t2 = FetchAsync("url2")
Dim t3 = FetchAsync("url3")

Dim results = Await Task.WhenAll(t1, t2, t3)
' All three run concurrently; results is an array of 3 strings

Dim winner = Await Task.WhenAny(t1, t2, t3)
' First to complete

Cancellation

Imports System.Threading

Async Function SlowOpAsync(token As CancellationToken) As Task(Of Integer)
    For i = 1 To 10
        token.ThrowIfCancellationRequested()
        Await Task.Delay(100, token)
    Next
    Return 42
End Function

Dim cts = New CancellationTokenSource()
cts.CancelAfter(500)        ' cancel after 500ms
Try
    Dim result = Await SlowOpAsync(cts.Token)
Catch ex As OperationCanceledException
    Console.WriteLine("cancelled")
End Try

Common patterns

Async file I/O:

Imports System.IO

Using reader As New StreamReader("data.txt")
    Dim text = Await reader.ReadToEndAsync()
End Using

Async loops with collections:

For Each url In urls
    Dim data = Await FetchAsync(url)
    Process(data)
Next
' Sequential — each waits for previous

' For parallelism:
Dim tasks = urls.Select(Function(u) FetchAsync(u))
Dim results = Await Task.WhenAll(tasks)

Why async in VB.NET

The same reasons as in any language:

  • Don't block threads on I/O (HTTP, DB, file)
  • Scale to many concurrent operations
  • Keep UIs responsive
  • Compose async pipelines

VB.NET adopted async/await around 2012 (alongside C# 5). Most modern VB.NET enterprise code uses it heavily, especially for ASP.NET / Web API code.

Why the exercise below is synchronous

Everything on this page is real VB.NET and none of it compiles in this course's editor. The grader runs vbnc, the Mono VB compiler that stopped at the VB 9/10 language level in 2010 — two years before Async/Await arrived. Feed it Async Function ... As Task(Of Integer) and it reports error VBNC90019: Expected 'End' on the Async keyword itself.

So the exercise asks for the synchronous shape that the async version replaces. That is a smaller loss than it sounds: the call graph is the part you are designing, and Async/Await changes the waiting, not the structure. Doubled(n) becomes Await DoubleAsync(n), As Integer becomes As Task(Of Integer), and the code above is what the result looks like on a current toolchain.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…