Skip to content

Commit b536d23

Browse files
committed
💾 Feat(QueueHelper): Now accept CancellationToken to control ForEachAsync.
1 parent b83d77e commit b536d23

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

Common.BasicHelper/Utils/Extensions/QueueHelper.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
namespace Common.BasicHelper.Utils.Extensions;
@@ -57,8 +58,12 @@ public static Queue<T> Pop<T>(this Queue<T> queue)
5758
/// <param name="reappend">是否将出队元素重新入队</param>
5859
/// <param name="locker">操作锁</param>
5960
/// <returns>队列本身</returns>
60-
public static Queue<T> ForEach<T>(this Queue<T> queue, Action<T> action,
61-
bool reappend = false, object? locker = null)
61+
public static Queue<T> ForEach<T>(
62+
this Queue<T> queue,
63+
Action<T> action,
64+
bool reappend = false,
65+
object? locker = null
66+
)
6267
{
6368
Queue<T> func()
6469
{
@@ -92,8 +97,12 @@ Queue<T> func()
9297
/// <param name="reappend">是否将出队元素重新入队</param>
9398
/// <param name="locker">操作锁</param>
9499
/// <returns>返回队列本身的任务</returns>
95-
public static async Task<Queue<T>> ForEachAsync<T>(this Queue<T> queue, Action<T> action,
96-
bool reappend = false)
100+
public static async Task<Queue<T>> ForEachAsync<T>(
101+
this Queue<T> queue,
102+
Action<T> action,
103+
bool reappend = false,
104+
CancellationToken token = default
105+
)
97106
{
98107
Queue<T> func()
99108
{
@@ -108,6 +117,6 @@ Queue<T> func()
108117
return queue;
109118
}
110119

111-
return await Task.Run(func);
120+
return await Task.Run(func, token);
112121
}
113122
}

0 commit comments

Comments
 (0)