private void SpinWork(long milliseconds) { int i = 0; int checkFactor = milliseconds < 50 ? 10 : 1000; var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < milliseconds) // spin for the duration { i++; if (i % checkFactor == 0 && this.Level < 20) { int coreId = GetCurrentProcessorNumber(); if (coreId != this.CoreId) { sw.Stop(); _hasSwichCore = true; Interlocked.CompareExchange(ref _endAt, _watch.ElapsedMilliseconds, 0); var spinDuration = _data.SpinDuration - sw.ElapsedMilliseconds; var unit = ParallelWorkUnitTrace.Create(_data, _heightFactor, _watch, _collection, _arrangeStrategy); // switch core unit._hasSwichCore = true; Child = unit; unit.Level = this.Level + 1; unit.IsParent = false; break; } } } sw.Stop(); }
public static ParallelWorkUnitTrace Create( DataItem data, double heightFactor, Stopwatch watch, IProducerConsumerCollection <ParallelWorkUnitTrace> collection, Func <ArrangeBy> arrangeStrategy) { var instance = new ParallelWorkUnitTrace(data, heightFactor, watch, collection, arrangeStrategy); instance.SpinWork(data.SpinDuration); instance.Close(); collection.TryAdd(instance); return(instance); }