liveBook · Manning

Async/await

As explained in .NET MAUI in Action

async/await is a C# language feature for composing asynchronous operations without blocking the calling thread while an operation is incomplete.

async/await lets a .NET MAUI app start asynchronous work without blocking the UI while that work is incomplete. This matters for long-running or failing operations: a locked-up app creates a poor user experience. Use an async method when work such as database setup must finish, but the app should remain responsive. section 3.3

A constructor cannot be async. Put asynchronous initialization in a separate async method, then call it with a discard (`_`) from the constructor so initialization can begin without blocking the UI. Await asynchronous database calls when their results are needed before continuing. section 3.3

Load to-do items after page creation

A page creates its Database instance, starts `Initialize()` with `_ = Initialize()`, and makes `Initialize` return `Task`. Inside it, `await _database.GetTodos()` retrieves stored items before the page formats them for display. The same application uses asynchronous SQLite operations to create tables and insert items. section 3.3

Listing 3.11 Code for the MauiTodo main page
using MauiTodo.Data;
using MauiTodo.Models;
 
namespace MauiTodo;
 
public partial class MainPage : ContentPage
{
    string _todoListData = string.Empty;                                          #1
 
    readonly Database _database;                                                  #2
 
    public MainPage()
    {
        InitializeComponent();
        _database = new Database();                                               #3
 
        _ = Initialize();                                                         #4
    }
 
    private async Task Initialize()
    {
        var todos = await _database.GetTodos();                                   #5
 
        foreach (var todo in todos)
        {
            _todoListData += $"{todo.Title} - {todo.
 Due:f}{Environment.NewLine}";                                                  #6
        }
 
           TodosLabel.Text = _todoListData;                                       #7
    }
 
    private async void Button_Clicked(object sender,EventArgs e)
    {
        var todo = new TodoItem                                                   #8
        {
            Due = DueDatepicker.Date,
            Title = TodoTitleEntry.Text
        };
 
        var inserted = await _database.AddTodo(todo);                             #9
 
        if (inserted != 0)                                                        #10
        {
            _todoListData += $"{todo.Title} - {todo.Due:f}{Environment.NewLine}"; #11
 
            TodosLabel.Text = _todoListData;                                      #12
                
            TodoTitleEntry.Text = String.Empty;                                   #13
            DueDatepicker.Date = DateTime.Now;                                    #13
        }
    }
}

Inspect the page constructor and `Initialize` method together to see a discarded initialization call followed by an awaited database query. section 3.3

How .NET MAUI in Action covers it

How deep it goes
Introduces constructor-safe async initialization and awaiting database results, while deferring broader UI async patterns and traps.
Examples
Uses the MauiTodo page to start Initialize and await GetTodos, alongside asynchronous table creation and item insertion.
What it assumes
Introduces UI-focused async usage while noting that .NET developers may already know async/await and asynchronous programming.
What you can do after
Apply it via .NET MAUI and SQLite-net to initialize and query app data without blocking the UI.
How it connects
Connects asynchronous programming with SQLite-net, constructors, POCOs, ORM operations, LINQ, and lambda expressions.

Also explained in

Compare how 17 books explain async/await →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.