private void StopCoverageProcess()
 {
     try
     {
         _reloadCoverageThread?.Abort();
     }
     catch
     {
         // ignore
     }
     finally
     {
         _reloadCoverageThread = null;
         FCCEngine.ClearProcesses();
     }
 }
        private void OperationState_StateChanged(object sender, OperationStateChangedEventArgs e)
        {
            try
            {
                if (e.State == TestOperationStates.TestExecutionStarting)
                {
                    try
                    {
                        _reloadCoverageThread?.Abort();
                    }
                    catch
                    {
                        // ignore
                    }
                    finally
                    {
                        _reloadCoverageThread = null;
                        FCCEngine.ClearProcesses();
                    }
                }

                if (e.State == TestOperationStates.TestExecutionFinished)
                {
                    var settings = AppOptions.Get();

                    if (!settings.Enabled)
                    {
                        FCCEngine.CoverageLines.Clear();
                        UpdateMarginTags?.Invoke(this, null);
                        UpdateOutputWindow?.Invoke(this, null);
                        return;
                    }

                    Logger.Log("================================== START ==================================");

                    var operationType     = e.Operation.GetType();
                    var darkMode          = CurrentTheme.Equals("Dark", StringComparison.OrdinalIgnoreCase);
                    var testConfiguration = (operationType.GetProperty("Configuration") ?? operationType.GetProperty("Configuration", BindingFlags.Instance | BindingFlags.NonPublic)).GetValue(e.Operation);
                    var testContainers    = ((IEnumerable <object>)testConfiguration.GetType().GetProperty("Containers").GetValue(testConfiguration)).ToArray();
                    var projects          = new List <CoverageProject>();

                    foreach (var container in testContainers)
                    {
                        var project           = new CoverageProject();
                        var containerType     = container.GetType();
                        var containerData     = containerType.GetProperty("ProjectData").GetValue(container);
                        var containerDataType = containerData.GetType();

                        project.ProjectGuid = containerType.GetProperty("ProjectGuid").GetValue(container).ToString();
                        project.ProjectName = containerType.GetProperty("ProjectName").GetValue(container).ToString();
                        project.TestDllFileInOutputFolder = containerType.GetProperty("Source").GetValue(container).ToString();
                        project.TestDllCompilationMode    = AssemblyUtil.GetCompilationMode(project.TestDllFileInOutputFolder);
                        project.ProjectFile = containerDataType.GetProperty("ProjectFilePath", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic).GetValue(containerData).ToString();

                        var defaultOutputFolder = Path.GetDirectoryName(containerDataType.GetProperty("DefaultOutputPath", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic).GetValue(containerData).ToString());
                        project.WorkFolder = Path.Combine(Path.GetDirectoryName(defaultOutputFolder), "fine-code-coverage");

                        projects.Add(project);
                    }

                    _reloadCoverageThread = new Thread(() =>
                    {
                        try
                        {
                            // compute coverage

                            FCCEngine.ReloadCoverage(projects.ToArray(), darkMode);

                            // update margins

                            {
                                UpdateMarginTagsEventArgs updateMarginTagsEventArgs = null;

                                try
                                {
                                    updateMarginTagsEventArgs = new UpdateMarginTagsEventArgs
                                    {
                                    };
                                }
                                catch
                                {
                                    // ignore
                                }
                                finally
                                {
                                    UpdateMarginTags?.Invoke(this, updateMarginTagsEventArgs);
                                }
                            }

                            // update output window

                            {
                                UpdateOutputWindowEventArgs updateOutputWindowEventArgs = null;

                                try
                                {
                                    updateOutputWindowEventArgs = new UpdateOutputWindowEventArgs
                                    {
                                        HtmlContent = File.ReadAllText(FCCEngine.HtmlFilePath)
                                    };
                                }
                                catch
                                {
                                    // ignore
                                }
                                finally
                                {
                                    UpdateOutputWindow?.Invoke(this, updateOutputWindowEventArgs);
                                }
                            }

                            // log

                            Logger.Log("================================== DONE ===================================");
                        }
                        catch (Exception exception)
                        {
                            if (!(exception is ThreadAbortException) && _reloadCoverageThread != null)
                            {
                                Logger.Log("Error", exception);
                                Logger.Log("================================== ERROR ==================================");
                            }
                        }
                    });

                    _reloadCoverageThread.Start();
                }
            }
            catch (Exception exception)
            {
                Logger.Log("Error processing unit test events", exception);
            }
        }