VB.NET Async/Await Documentation

Overview

The Async and Await keywords simplify asynchronous programming in Visual Basic. They enable you to write non‑blocking code that reads like synchronous code, improving responsiveness and scalability.

Getting Started

Creating an Async Method

Public Async Function GetDataAsync(ByVal url As String) As Task(Of String)
    Using client As New HttpClient()
        Dim response As String = Await client.GetStringAsync(url)
        Return response
    End Using
End Function

Calling an Async Method

Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles btnLoad.Click
    Dim result As String = Await GetDataAsync("https://api.example.com/data")
    txtOutput.Text = result
End Sub

Key Concepts

Best Practices

  1. Prefer Async all the way down the call stack.
  2. Avoid blocking calls like .Result or .Wait() on tasks.
  3. Use ConfigureAwait(False) in library code to avoid deadlocks.

Related Topics