using Serilog; namespace DispenserCommon.Scheduler; /// /// 延时定时任务 /// public class DelayScheduler { /// /// 设定延时任务 /// /// /// /// /// /// public static async void Delay(Action action, TimeSpan delay, CancellationToken cancellationToken = default) { try { if (action == null) throw new ArgumentNullException(nameof(action)); if (delay.TotalMilliseconds < 0) throw new ArgumentOutOfRangeException(nameof(delay), "延时时间不能为负数"); await Task.Delay(delay, cancellationToken); if (cancellationToken.IsCancellationRequested) { return; } action(); } catch (Exception e) { Log.Error(e, "延时任务执行失败"); } } }