Skip to content

Instantly share code, notes, and snippets.

@xakpc
Created June 5, 2022 18:30
Show Gist options
  • Save xakpc/a5621a3147a6b8fc48e66b1611538973 to your computer and use it in GitHub Desktop.
Save xakpc/a5621a3147a6b8fc48e66b1611538973 to your computer and use it in GitHub Desktop.
a simple countdown timer with dotnet Blazor
<div class = "clock">
<h1>@Time</h1>
</div>
using System.Timers;
using Microsoft.AspNetCore.Components;
public partial class Countdown : ComponentBase, IDisposable
{
private System.Timers.Timer _timer = null!;
private int _secondsToRun = 0;
protected string Time { get; set; } = "00:00";
[Parameter]
public EventCallback TimerOut { get; set; }
public void Start(int secondsToRun)
{
_secondsToRun = secondsToRun;
if (_secondsToRun > 0)
{
Time = TimeSpan.FromSeconds(_secondsToRun).ToString(@"mm\:ss");
StateHasChanged();
_timer.Start();
}
}
public void Stop()
{
_timer.Stop();
}
protected override void OnInitialized()
{
_timer = new System.Timers.Timer(1000);
_timer.Elapsed += OnTimedEvent;
_timer.AutoReset = true;
}
private async void OnTimedEvent(object? sender, ElapsedEventArgs e)
{
_secondsToRun--;
await InvokeAsync(() =>
{
Time = TimeSpan.FromSeconds(_secondsToRun).ToString(@"mm\:ss");
StateHasChanged();
});
if (_secondsToRun <= 0)
{
_timer.Stop();
await TimerOut.InvokeAsync();
}
}
public void Dispose()
{
_timer.Dispose();
}
}
.clock {
color: var(--color-tone-1);
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
overflow: hidden;
}
@N1koV373z
Copy link

I have a question! What does the public EventCallback TimerOut { get; set; } here?

@xakpc
Copy link
Author

xakpc commented Mar 3, 2023

I have a question! What does the public EventCallback TimerOut { get; set; } here?

it allows you to do some actions on the timer completion
when used like this

<Countdown @ref="timer" TimerOut="TimerOutCallback"/>
...
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            timer.Start(10);
        }
    }

    protected Shared.Countdown timer;

    private void TimerOutCallback()
    {
        ...
    }

@gcnabeel
Copy link

This is really neat! Thanks for sharing it.

@PWSALLITC
Copy link

This looks like a great solution. I am newbie with Blazor. Created tree but getting these errors - stumped maybe you can point me in the right direction to solve?

Severity Code Description Project Path File Line Suppression State
Error (active) CS0103 The name 'Time' does not exist in the current context RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb\Components\Pages C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb\Components\Pages\Countdown.razor 2

Error (active) RZ3008 Tag helpers cannot target tag name '.Countdown' because it contains a '<' character. RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb\CSC 1

Error (active) RZ3008 Tag helpers cannot target tag name '.Countdown' because it contains a ' ' character. RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb\CSC 1

Error (active) RZ3008 Tag helpers cannot target tag name '.Countdown' because it contains a '>' character. RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb C:\VisualStudio2022\Projects\RASPublicWEB\RASPublicWeb\CSC 1

@PWSALLITC
Copy link

Cleaned project and solution and closed and re-opened to no avail.

@yhbder
Copy link

yhbder commented Sep 27, 2024

image

I got Error when call back fire:

System.InvalidOperationException: 'The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state.'

@yhbder
Copy link

yhbder commented Sep 27, 2024

I resolved after i but this code :
await InvokeAsync(() =>
{

TimerOut.InvokeAsync();

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment