public bool StopCounter(int id)
        {
            PerformanceMonitorRecord recordToUpdate =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == id);

            string performanceCounterReading =
                _performanceCounterService.GetPerformanceCounterReading(recordToUpdate.CategoryName, recordToUpdate.InstanceName, recordToUpdate.CounterName);

            //threshold check
            bool   passedThreshold;
            var    passedThresholdValue = 0;
            double countervalue         = double.Parse(performanceCounterReading);

            passedThreshold = _performanceMonitorService.CheckThreshold(recordToUpdate.Threshold, recordToUpdate.ThresholdWhen, countervalue, ref passedThresholdValue);

            //calculate the the duration (in ticks)
            DateTime startTime     = (DateTime)recordToUpdate.Created;
            TimeSpan durationCount = DateTime.Now.Subtract(startTime);

            DateTime sharedTimeStampValue = DateTime.Now;

            //first write last values to datarecord (then to the monitorrecord for min/max/average)
            PerformanceMonitorDataRecord datarecord = new PerformanceMonitorDataRecord()
            {
                PerformanceMonitorRecord_Id = recordToUpdate.Id,
                //should be finally equal to latest reading in performancemonitorrecord
                Count                = performanceCounterReading,
                HeartBeat            = sharedTimeStampValue,
                PassedThreshold      = passedThreshold,
                PassedThresholdValue = passedThresholdValue,
                //duration count(ticks in nanoseconds from starttime (created)) should be equal to value in performancemonitorrecord
                Ticks = Convert.ToString(durationCount.Ticks)
            };

            var result = _performanceMonitorDataService.WriteDataRecord(datarecord);

            if (result)
            {
                recordToUpdate.LastValue     = performanceCounterReading;
                recordToUpdate.DurationCount = Convert.ToString(durationCount.Ticks);
                recordToUpdate.EndTime       = sharedTimeStampValue;

                //mean, min and max values
                List <PerformanceMonitorDataRecord> dataRecords = _performanceMonitorDataService.GetDataRecords(id);
                //first convert counted values from string to int
                List <double> countedValues    = new List <double>();
                bool          conversionResult = false;
                foreach (PerformanceMonitorDataRecord record in dataRecords)
                {
                    double value;
                    bool   conversionPassed = double.TryParse(record.Count, out value);
                    if (conversionPassed)
                    {
                        countedValues.Add(value);
                        conversionResult = true;
                    }
                    else
                    {
                        //conversion failed
                        conversionResult = false;
                    }
                }
                double meanValue = 0;
                double minValue  = 0;
                double maxValue  = 0;

                if (conversionResult)
                {
                    meanValue = Math.Round(countedValues.Average(), 2);
                    minValue  = Math.Round(countedValues.Min(), 2);
                    maxValue  = Math.Round(countedValues.Max(), 2);
                }
                recordToUpdate.Mean    = meanValue;
                recordToUpdate.Minimum = minValue;
                recordToUpdate.Maximum = maxValue;

                var monitorrecordresult = _performanceMonitorDataService.UpdatePerformanceMonitorRecord(recordToUpdate);
                if (monitorrecordresult)
                {
                    _notifier.Information(T("Counter stopped and all values updated succesfully"));
                }
                else
                {
                    _notifier.Error(T("Error: Failed to stop counter and update the monitor record"));
                }
            }
            else
            {
                _notifier.Error(T("Error: Failed to stop counter and update all records"));
            }

            return(result);
        }