Skip to content

Instantly share code, notes, and snippets.

@ctrl-alt-d
Created March 2, 2021 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrl-alt-d/faf558b42bdb78f3cb3ffd2a9f9dc80a to your computer and use it in GitHub Desktop.
Save ctrl-alt-d/faf558b42bdb78f3cb3ffd2a9f9dc80a to your computer and use it in GitHub Desktop.
Blazor server sending 500 upates per second
@page "/"
Tics per second: <input type="range" min="1" max="500" @bind="@CurrentValue" class="slider" id="myRange"> @CurrentValue
<br/><br/>
<div style="width:500px; height:10px; background-color: blue; position: relative;">
<div class="ball" style="@position_txt"></div>
</div>
<br/><br/>
<span>@DateTime.Now.ToString("HH:mm:ss")</span>
<span>Number of renders: @nRenders.ToString("N0")</span>
<br/><br/>
<button type="button" @onclick="start">start</button>
<style>
.ball {
width: 30px; height: 30px;
top: -10px;
position: absolute;
background-color: blue;
}
</style>
@code
{
Int64 nRenders = 0, v = 1, position = 10, CurrentValue = 10;
string position_txt => $"left: {position}px;";
private static System.Timers.Timer aTimer = new System.Timers.Timer();
protected void start()
{
move();
aTimer.Elapsed += (source, e) => move();
aTimer.AutoReset = true;
aTimer.Enabled = !aTimer.Enabled;
}
protected void move()
{
aTimer.Interval = 1000.0/CurrentValue;
position = (position+v);
if (position>500 || position<0) v *= -1;
InvokeAsync(StateHasChanged);
}
protected override void OnAfterRender(bool firstRender) => nRenders++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment