Skip to content

Commit e3aab71

Browse files
committed
util: 添加时段扩展方法
1 parent 5f7f3c2 commit e3aab71

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/OSharp.Utils/Timing/TimeSpanExtensions.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// -----------------------------------------------------------------------
1+
// -----------------------------------------------------------------------
22
// <copyright file="TimeSpanExtensions.cs" company="OSharp开源团队">
33
// Copyright (c) 2014-2020 OSharp. All rights reserved.
44
// </copyright>
@@ -53,12 +53,16 @@ public static string ToTimeString(this TimeSpan ts)
5353
/// <summary>
5454
/// 时间片倒计时
5555
/// </summary>
56-
public static void CountDown(this TimeSpan ts, Action<TimeSpan> action, int intervalMilliseconds = 1000)
56+
public static void CountDown(this TimeSpan ts, Action<TimeSpan> action, int intervalMilliseconds = 1000, Func<bool> breakFunc = null)
5757
{
5858
while (ts > TimeSpan.Zero)
5959
{
60+
if (breakFunc != null && breakFunc())
61+
{
62+
break;
63+
}
6064
action(ts);
61-
TimeSpan ts2 = TimeSpan.FromMilliseconds(intervalMilliseconds);
65+
var ts2 = TimeSpan.FromMilliseconds(intervalMilliseconds);
6266
Thread.Sleep(ts2);
6367
ts = ts.Subtract(ts2);
6468
}
@@ -67,15 +71,19 @@ public static void CountDown(this TimeSpan ts, Action<TimeSpan> action, int inte
6771
/// <summary>
6872
/// 时间片倒计时
6973
/// </summary>
70-
public static async Task CountDownAsync(this TimeSpan ts, Func<TimeSpan, Task> action, int intervalMilliseconds = 1000)
74+
public static async Task CountDownAsync(this TimeSpan ts, Func<TimeSpan, Task> action, int intervalMilliseconds = 1000, Func<Task<bool>> breakFunc = null)
7175
{
7276
while (ts > TimeSpan.Zero)
7377
{
78+
if (breakFunc != null && await breakFunc())
79+
{
80+
break;
81+
}
7482
await action(ts);
7583
TimeSpan ts2 = TimeSpan.FromMilliseconds(intervalMilliseconds);
7684
await Task.Delay(ts2);
7785
ts = ts.Subtract(ts2);
7886
}
7987
}
8088
}
81-
}
89+
}

0 commit comments

Comments
 (0)