public CapturedMetric[] GetMetrics() { CapturedMetadata[] profiledMethodsCopy; CapturedMetric[] metrics; lock (registrationLock) { profiledMethodsCopy = Metadata; metrics = new CapturedMetric[ProfiledMethodCount]; } var treadLocalCollectorsCopy = _collectors.Values; var timestamp = ReferenceFrame.Now(); for (var i = 0; i < metrics.Length; i++) { var method = profiledMethodsCopy[i]; var attempts = 0; while (true) { metrics[i].Timestamp = timestamp.Ticks; foreach (var threadLocalCollector in treadLocalCollectorsCopy) { if (threadLocalCollector.GetSample(method, out var threadLocalData)) { metrics[i].AddData(threadLocalData); } } timestamp = ReferenceFrame.Now(); // Detect if our thread has been preempted, and retry if so. if (timestamp.Ticks - metrics[i].Timestamp < timestampTolerance) { break; } else { metrics[i] = default; attempts++; if (attempts > 3) { Console.WriteLine("Too many non-voluntary preemptions in the publisher thread. "); metrics[i].SetInvalid(); break; } } } } return(metrics); }