using Castle.Core.Internal; using DispenserCommon.Atrributes; using Serilog; namespace DispenserCommon.Scheduler; public class TaskWaiter { /// /// 在 /// /// /// /// /// public static async Task WaitingFor(Func action, long timeout = 60000, CancellationTokenSource? cts = null) { var method = action.Method; var blockTask = method.GetAttribute(); var taskName = blockTask?.Name ?? method.Name; var span = TimeSpan.FromMilliseconds(timeout); var endTime = DateTime.Now.Add(span); return await Task.Run(() => { while (DateTime.Now < endTime) { if (cts is { IsCancellationRequested: true }) { return false; } if (action()) { Log.Information($"{taskName} 任务等待完成"); return true; } Thread.Sleep(200); } return false; }); } }