Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to handle exception from timer in blazor?

$
0
0

How to i correclty handle an exception thrown in another Thread (from Timer) in Blazor Server-Side?

Ideally i want the error to bubbly like all the other exceptions and trigger the yellow error bar.

enter image description here

Unfortunatly the exception terminates the whole application. I can put an try catch around the methode call, but would prefere to handle this more globally.

This is the code i run, inside the Elapsed-Handler (HandleSearchTimerElapsed) an Exception is thrown. This terminates the application, which i obviosly dont want.

@page "/"<button class="btn btn-primary" @onclick="ResetDelayTimer">Throw Error with Timer</button><button class="btn btn-primary" @onclick="@(() => throw new Exception("Simple Error"))">Simple Error without Timer (shows yellow error bar)</button>@code {    private int currentCount = 0;    private System.Timers.Timer? _delayTimer;    protected override void OnInitialized()    {        _delayTimer = new(200) { AutoReset = false, };        _delayTimer.Elapsed += HandleSearchTimerElapsed;        base.OnInitialized();    }    private async void HandleSearchTimerElapsed(object? sender, System.Timers.ElapsedEventArgs args)    {        try        {            await InvokeAsync(() => throw new Exception("From InvokeAsync in Timer"));        }        catch (Exception)        {            // How to throw this "normally" ? i want to see the yellow exception bar            throw;  // This terminates the Application        }    }    private void ResetDelayTimer()    {        _delayTimer!.Stop();        _delayTimer.Start();    }}

Can i trigger the "normal" exception handler from blazor?


Viewing all articles
Browse latest Browse all 4839

Trending Articles