public static MvSpinWait New(int?maxWaitMs = null, int?minWaitMs = null) { var mv = new MvSpinWait(); mv.SetWait(maxWaitMs, minWaitMs); return(mv); }
/// <summary> /// /// </summary> /// <param name="condition"></param> /// <param name="maxWaitMs">-1 無限等待</param> /// <param name="minWaitMs">-1 不等待</param> /// <param name="success"></param> /// <param name="fail"></param> /// <returns></returns> public static bool SpinUntil(Func <bool> condition, int maxWaitMs, int minWaitMs, Action success = null, Action fail = null) { var mvSpin = new MvSpinWait(); var flag = mvSpin.WaitUntil(condition, maxWaitMs, minWaitMs); if (flag) { if (success != null) { success(); } } else { if (fail != null) { fail(); } } return(flag); }
public static MvCancelTask RunLoop(Func <bool> funcIsContinue, int delay_ms = 0) { var task = new MvCancelTask(); var ct = task.CancelTokenSource.Token; task.Task = Task.Factory.StartNew(() => { while (!ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); if (!funcIsContinue()) { break; } if (delay_ms > 0) { MvSpinWait.SpinUntil(() => ct.IsCancellationRequested, delay_ms); } } }, ct); return(task); }