Skip to content

Instantly share code, notes, and snippets.

@jTakasuRyuji
Created April 1, 2024 21:40
Show Gist options
  • Select an option

  • Save jTakasuRyuji/e46687df6e865567e2a3187d1b9b8780 to your computer and use it in GitHub Desktop.

Select an option

Save jTakasuRyuji/e46687df6e865567e2a3187d1b9b8780 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
class Program
{
static async Task Main(string[] args)
{
int countdownSeconds = 60; // カウントダウンする秒数
// ストップウォッチを開始
Stopwatch stopwatch = Stopwatch.StartNew();
// カウントダウン
while (countdownSeconds > 0)
{
// 現在の経過時間を取得
long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
// 次の秒のタイムスタンプを計算
long nextSecondTimestamp = (elapsedMilliseconds / 1000 + 1) * 1000;
int MillisecondsDelay = (int)(nextSecondTimestamp - elapsedMilliseconds);
// 次の秒まで待機
await Task.Delay(MillisecondsDelay);
// 秒数を減算して表示
countdownSeconds--;
Console.WriteLine($"残り時間: {countdownSeconds}秒 / Task.Delay({MillisecondsDelay}ms)");
}
Console.WriteLine("タイマー終了");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment