Files
wcs/货架标准上位机/Tool/While.cs
2025-01-08 15:51:28 +08:00

45 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace
{
/// <summary>
/// 循环、暂停、继续、停止扩展
/// </summary>
public static class While
{
/// <summary>
/// 带条件等待并检测停止、暂停
/// </summary>
/// <param name="func">不停的执行的任务返回为true就跳出循环</param>
public static async Task Wait(Func<bool> func, CancellationToken token = default, ManualResetEvent manualReset = default)
{
while (true)
{
token.ThrowIfCancellationRequested();
manualReset?.WaitOne();
token.ThrowIfCancellationRequested();
if (func.Invoke())
break;
await Task.Delay(200, token);
}
}
/// <summary>
/// 等待并检测停止、暂停
/// </summary>
public static void Wait(CancellationToken token = default, ManualResetEvent manualReset = default)
{
token.ThrowIfCancellationRequested();
manualReset?.WaitOne();
token.ThrowIfCancellationRequested();
}
}
}