Skip to content

[Question] .NET 9 MAUI sample app with sample content async lock for DisplayAlert (why?) #28511

@janseris

Description

@janseris

Image

Hi, I noticed the new VS project template for MAUI for .NET 9 has the following code:

Why is async lock (SemaphoreSlim) used here for Shell.DisplayAlert?

/// <summary>
/// Modal Error Handler.
/// </summary>
public class ModalErrorHandler : IErrorHandler
{
    SemaphoreSlim _semaphore = new(1, 1);

    /// <summary>
    /// Handle error in UI.
    /// </summary>
    /// <param name="ex">Exception.</param>
    public void HandleError(Exception ex)
    {
        DisplayAlert(ex).FireAndForgetSafeAsync();
    }

    async Task DisplayAlert(Exception ex)
    {
        try
        {
            await _semaphore.WaitAsync();
            if (Shell.Current is Shell shell)
                await shell.DisplayAlert("Error", ex.Message, "OK");
        }
        finally
        {
            _semaphore.Release();
        }
    }
}

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-templatesProject templates, Item Templates for Blazor and MAUIpartner/syncfusionIssues / PR's with Syncfusion collaboration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions