private DetectionOutcome RunDetection()
 {
     try
     {
         detector.RunDetection();
     }
     catch (Exception exception)
     {
         return(new DetectionOutcome(DetectionStatus.Error, exception));
     }
     return(new DetectionOutcome(DetectionStatus.Finished, null));
 }
        private void StartDetection()
        {
            cts = new CancellationTokenSource();
            var token = cts.Token;

            token.Register(() =>
            {
                taskCanceled = true;
            });

            DetectContext context = new DetectContext((instanceId, stepId, logStyle) =>
            {
                if (taskCanceled || !instanceId.Equals(latestDetectorInstanceId))
                {
                    return;
                }

                var status = logStyle switch
                {
                    LogStyle.Default => DetectingStatus.Detecting,
                    LogStyle.Error => DetectingStatus.Error,
                    LogStyle.StepFailed => DetectingStatus.Failed,
                    LogStyle.StepSkipped => DetectingStatus.Skipped,
                    LogStyle.StepNotFound => DetectingStatus.NotFound,
                    LogStyle.StepPassed => DetectingStatus.Finished,
                    _ => DetectingStatus.Finished,
                };

                StepIndex = stepId;
                SetDetectStepCurrentStatus(status);
            }, token);

            latestDetectorInstanceId = context.Id;

            detectTask = new Task(() =>
            {
                token.ThrowIfCancellationRequested();

                // mark detection status as InProgress
                SetDetectionStatus(DetectionStatus.InProgress);
                try
                {
                    var resultStatus = ValueDetector.RunDetection(context) ? DetectionStatus.Finished : DetectionStatus.Error;
                    SetDetectionStatus(resultStatus);
                    detectedException = null;

                    if (cts.IsCancellationRequested)
                    {
                        SetDetectStepCurrentStatus(DetectingStatus.Cancelled);
                    }
                }
                catch (Exception ex)
                {
                    SetDetectionStatus(DetectionStatus.Error);
                    detectedException = ex;
                }

                if ((StepIndex < GetDetectedSteps().Count - 1) && !cts.IsCancellationRequested)
                {
                    SetDetectStepCurrentStatus(DetectingStatus.Pending);
                }

                CloseLogger();
            }, token);

            latestLogPath = Path.Combine(TestSuite.StorageRoot.AbsolutePath, "Detector_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff") + ".log");
            LogWriter     = new StreamWriter(latestLogPath);
            detectTask.Start();
        }