| | 1 | | @using System.Timers |
| | 2 | | @implements IDisposable |
| | 3 | |
|
| | 4 | | <div class="container"> |
| | 5 | | <div class="row"> |
| | 6 | | <div class="col"> |
| | 7 | | <h2><i class="fas fa-stopwatch"></i> Stopwatch</h2> |
| | 8 | | </div> |
| | 9 | | </div> |
| | 10 | |
|
| | 11 | | <div class="row"> |
| | 12 | | <div class="col"> |
| | 13 | | <input id="input" name="input" class="form-control" @bind="Input" placeholder="00:00" /> |
| | 14 | | </div> |
| | 15 | | </div> |
| | 16 | |
|
| | 17 | | <div class="row"> |
| | 18 | | <div class="col"> |
| | 19 | | <button id="btnStartStop" name="btnStartStop" class="btn btn-success" @onclick="StartStop"><i class="fas fa- |
| | 20 | | <button id="btnReset" name="btnReset" class="btn btn-info float-right" @onclick="Reset"><i class="fas fa-syn |
| | 21 | | </div> |
| | 22 | | </div> |
| | 23 | |
|
| | 24 | | @* <div class="row"> *@ |
| | 25 | | @* <div class="col"> *@ |
| | 26 | | @* <p>Inspired from <a href="" target="_blank"></a></p> *@ |
| | 27 | | @* </div> *@ |
| | 28 | | @* </div> *@ |
| | 29 | |
|
| | 30 | | </div> |
| | 31 | |
|
| | 32 | | @code { |
| | 33 | | [Parameter] |
| 0 | 34 | | public string? Input { get; set; } |
| | 35 | |
|
| | 36 | | [Parameter] |
| 0 | 37 | | public EventCallback TimerOut { get; set; } |
| | 38 | |
|
| 0 | 39 | | private Timer timer = null!; |
| 0 | 40 | | private int secondsToRun = 0; |
| | 41 | |
|
| | 42 | | // protected override void OnInitialized() |
| | 43 | | // { |
| | 44 | | // // Input = "00:00"; |
| | 45 | | // timer = new System.Threading.Timer(async _ => // async void |
| | 46 | | // { |
| | 47 | | // Input = |
| | 48 | | // // we need StateHasChanged() because this is an async void handler |
| | 49 | | // // we need to Invoke it because we could be on the wrong Thread |
| | 50 | | // await InvokeAsync(StateHasChanged); |
| | 51 | | // }, null, 0, 1000); |
| | 52 | | // } |
| | 53 | |
|
| | 54 | | void StartStop() |
| 0 | 55 | | { |
| 0 | 56 | | if (string.IsNullOrEmpty(Input)) return; |
| 0 | 57 | | Start(10); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | void Reset() |
| 0 | 61 | | { |
| 0 | 62 | | Start(600); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Start(int secondsToRun) |
| 0 | 66 | | { |
| 0 | 67 | | if (secondsToRun > 0) |
| 0 | 68 | | { |
| 0 | 69 | | Input = TimeSpan.FromSeconds(secondsToRun).ToString(@"mm\:ss"); |
| 0 | 70 | | StateHasChanged(); |
| 0 | 71 | | timer.Start(); |
| 0 | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Stop() |
| 0 | 76 | | { |
| 0 | 77 | | timer.Stop(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | protected override void OnInitialized() |
| 0 | 81 | | { |
| 0 | 82 | | timer = new Timer(1000); |
| 0 | 83 | | timer.Elapsed += OnTimedEvent; |
| 0 | 84 | | timer.AutoReset = true; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private async void OnTimedEvent(object? sender, ElapsedEventArgs e) |
| 0 | 88 | | { |
| 0 | 89 | | secondsToRun--; |
| | 90 | |
|
| 0 | 91 | | await InvokeAsync(() => |
| 0 | 92 | | { |
| 0 | 93 | | Input = TimeSpan.FromSeconds(secondsToRun).ToString(@"mm\:ss"); |
| 0 | 94 | | StateHasChanged(); |
| 0 | 95 | | }); |
| | 96 | |
|
| 0 | 97 | | if (secondsToRun <= 0) |
| 0 | 98 | | { |
| 0 | 99 | | timer.Stop(); |
| 0 | 100 | | await TimerOut.InvokeAsync(); |
| 0 | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void Dispose() |
| 0 | 105 | | { |
| 0 | 106 | | timer.Dispose(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | } |