CSharp中的倒计时

2023-11-16 09:30:32 浏览数 (2)

简单示例

代码语言:javascript复制
private void Countdown()
{
    // 设置倒计时时间(以毫秒为单位)
    int countdownTime = 5000;
    Stopwatch stopwatch = Stopwatch.StartNew();
    while (stopwatch.ElapsedMilliseconds < countdownTime)
    {
        // 剩余时间 = 倒计时时间 - 已经流逝的时间
        int remainingTime = countdownTime - (int)stopwatch.ElapsedMilliseconds;

        // 输出剩余时间
        Console.WriteLine(
            @"Remaining Time: {0} milliseconds",
            remainingTime
        );

        // 可以根据需要选择适当的时间间隔,这里选择1秒
        Thread.Sleep(1000);
    }
    Console.WriteLine(@"Countdown finished!");
}

项目示例

代码语言:javascript复制
private bool _isShowMinuteWin;
private bool _isShowSecWin;
private MessageWinMinute _messageWinMinute;
private MessageWinSec _messageSecWinSec;

private void Countdown()
{
    new Thread(
        () =>
        {
            // 设置倒计时时间(以毫秒为单位)
            int countdownTime = ZCommonData.ClassTotalSec * 1000;
            Stopwatch stopwatch = Stopwatch.StartNew();
            while (stopwatch.ElapsedMilliseconds <= countdownTime)
            {
                // 剩余时间(秒) = 倒计时时间 - 已经流逝的时间
                int remainingSec = (countdownTime - (int)stopwatch.ElapsedMilliseconds) / 1000;

                // 输出剩余时间
                Console.WriteLine(
                    @"Remaining Time: {0} seconds",
                    remainingSec
                );
                //当剩余时间小于等于3分钟 并且提示窗未弹出 则弹出提示窗
                if (remainingSec <= 10)
                {
                    Dispatcher.Invoke(
                        () =>
                        {
                            if (!this._isShowSecWin)
                            {
                                this._isShowSecWin = true;
                                this._messageSecWinSec = new MessageWinSec();
                                this._messageSecWinSec.Show();
                                this._messageSecWinSec.SetSec(remainingSec);
                                this._messageWinMinute?.Close();
                            }
                            this._messageSecWinSec?.SetSec(remainingSec);
                        }
                    );
                }
                else if (remainingSec <= 3 * 60)
                {
                    Dispatcher.Invoke(
                        () =>
                        {
                            if (!this._isShowMinuteWin)
                            {
                                this._isShowMinuteWin = true;
                                this._messageWinMinute = new MessageWinMinute();
                                this._messageWinMinute.Show();
                                this._messageWinMinute.SetSec(remainingSec);
                            }
                            this._messageWinMinute?.SetSec(remainingSec);
                        }
                    );
                }

                // 可以根据需要选择适当的时间间隔,这里选择1秒
                Thread.Sleep(1000);
            }
            Dispatcher.Invoke(this.CloseAction);
        }
    ).Start();
}

0 人点赞