'How to handle exception from timer in blazor?
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.
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

