private int recordMedianFingerCount(Stopwatch timer, double duration, double overallDuration, uint targetId, CancellationToken ct) { var leftHand = Options.SelectedJoints[0].Main == FubiUtils.SkeletonJoint.LEFT_HAND; var recordedFingerCounts = new List <int>(); do { if (ct.IsCancellationRequested) { setDescription("Cancelled!"); return(-1); } if (UseHand) { recordedFingerCounts.Add(Fubi.getHandFingerCount(targetId)); } else { recordedFingerCounts.Add(Fubi.getFingerCount(targetId, leftHand, false)); } decrementDuration(overallDuration, timer); // With 10 ms sleep we should still catch all different values, but we don't get too many Thread.Sleep(10); } while (timer.Elapsed < TimeSpan.FromSeconds(duration)); recordedFingerCounts.Sort(); return(recordedFingerCounts[recordedFingerCounts.Count / 2]); }