/// <summary> /// 输出养分 /// </summary> /// <param name="nutrient"></param> private void OutputNutrient(N nutrient) { if (!_outputElapsedStopwatch.IsRunning) { _outputElapsedStopwatch.Restart(); } RaiseOutputtedEvent(nutrient); int nutrientOutputInterval = GetNutrientOutputInterval(); if (nutrientOutputInterval > 0) { long sleep = 0; unchecked { sleep = nutrientOutputInterval - _outputElapsedStopwatch.ElapsedMilliseconds; } if (sleep > 0) { //var raw = _outputElapsedStopwatch.ElapsedMilliseconds; //Debug.WriteLine("sleep:" + sleep); _delapyer.Sleep((int)sleep); //Debug.WriteLine("actualSleep:" + (_outputElapsedStopwatch.ElapsedMilliseconds - raw)); } } _outputElapsedStopwatch.Restart(); }
private void InternalAction() { try { while (!_cancellation.IsCancellationRequested) { _action(this); _delapyer.Sleep(_intervalMilliseconds); } } catch (ThreadAbortException) { } catch (TaskCanceledException) { } catch (Exception e) { _catchException(e); } }
public void HighResolutionSleep() { const int times = 100; //运行次数 const int sleepTime = 10; //毫秒 Stopwatch watch = new Stopwatch(); MediaDelayer delayer = new MediaDelayer(); long elapsed = 0; for (var i = 0; i < times; i++) { watch.Restart(); delayer.Sleep(sleepTime); elapsed += watch.ElapsedMilliseconds; } NumericalRangeInt nri = new NumericalRangeInt(sleepTime, 0.2f); var avg = (int)(elapsed / times); Assert.IsTrue(nri.Contains(avg)); }