CSharp中的倒计时(Stopwatch/DispatcherTimer/TimeSpan)

Stopwatch

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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!");
}

项目示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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();
}

UI线程的计时

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
TimeSpan _timeSpan = TimeSpan.FromMinutes(3);

DispatcherTimer _dTimer = new DispatcherTimer();
_dTimer.Tick += OnTimer;
_dTimer.Interval = TimeSpan.FromSeconds(1);
_dTimer.IsEnabled = true;
_dTimer.Start();

private void OnTimer(object sender, EventArgs e)
{
_timeSpan -= new TimeSpan.FromSeconds(1);
}

日期处理

1
string time = $"{_timeSpan.Minutes:D2}{_timeSpan.Seconds:D2}";

TimeSpan

在 C# 中,可以使用多种方式来初始化 TimeSpan 类的实例。

初始化

以下是常用的几种参数初始化方式:

使用时间单位的数值初始化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 初始化为 10 秒
// TimeSpan(int hours, int minutes, int seconds)
TimeSpan timeSpan1 = new TimeSpan(0, 0, 10);

// 初始化为 1 小时 30 分钟
TimeSpan timeSpan2 = new TimeSpan(1, 30, 0);

// 初始化为 2 天 3 小时 15 分钟 30 秒
// TimeSpan(int days, int hours, int minutes, int seconds)
TimeSpan timeSpan3 = new TimeSpan(2, 3, 15, 30);

// 初始化为 1 天 3 小时 15 分 30 秒 500 毫秒
// TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
TimeSpan timeSpan4 = new TimeSpan(1, 3, 15, 30, 500);

其中还有一个参数的方法,不建议使用。

使用总的时间间隔数值初始化:

1
2
3
4
5
6
7
8
9
10
11
// 初始化为 10 秒
TimeSpan timeSpan1 = TimeSpan.FromSeconds(10);

// 初始化为 1 小时 30 分钟
TimeSpan timeSpan2 = TimeSpan.FromMinutes(90);

// 初始化为 2 天 3 小时 15 分钟 30 秒
TimeSpan timeSpan3 = TimeSpan.FromDays(2) + TimeSpan.FromHours(3) + TimeSpan.FromMinutes(15) + TimeSpan.FromSeconds(30);

// 初始化为 1 小时 15 分钟 30 秒 500 毫秒
TimeSpan timeSpan4 = TimeSpan.FromHours(1) + TimeSpan.FromMinutes(15) + TimeSpan.FromSeconds(30) + TimeSpan.FromMilliseconds(500);

使用 TimeSpan.Parse 方法:

1
2
3
4
5
// 使用字符串 "10:30" 初始化为 10 小时 30 分钟
TimeSpan timeSpan1 = TimeSpan.Parse("10:30");

// 使用字符串 "1.05:30:45.500" 初始化为 1 天 5 小时 30 分钟 45 秒 500 毫秒
TimeSpan timeSpan2 = TimeSpan.Parse("1.05:30:45.500");

这些是一些常用的初始化 TimeSpan 的方式,根据具体的需求选择适合的方式来初始化。

同时需要注意,TimeSpan 是一个不可变的结构体,一旦初始化后,其值是不能修改的,可以通过运算符和方法来对 TimeSpan 进行加减运算、比较等操作。

格式化

1
2
3
TimeSpan timeSpan2 = TimeSpan.Parse("1.23:30:45.500");
// 23小时30分钟45秒
Console.WriteLine(timeSpan2.ToString("hh'小时'mm'分钟'ss'秒'"));

获取总时长

1
2
3
4
5
6
TimeSpan timeSpan2 = TimeSpan.Parse("1.05:30:45.500");
Console.WriteLine(timeSpan2.TotalDays);
Console.WriteLine(timeSpan2.TotalHours);
Console.WriteLine(timeSpan2.TotalMinutes);
Console.WriteLine(timeSpan2.TotalSeconds);
Console.WriteLine(timeSpan2.TotalMilliseconds);

结果

1.22969328703704
29.5126388888889
1770.75833333333
106245.5
106245500

时间戳

1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// 返回一个时间戳到秒
/// </summary>
/// <returns>
/// </returns>
public static long TimestampTotalSeconds()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timestr = Convert.ToInt64(ts.TotalSeconds);
return timestr;
}