public void StartAnalyzer()
            {
                var analyzerThread = new Thread(() =>
                {
                    try
                    {
                        AnalyzerChangeState(State.Running);
                        while (State == State.Running)
                        {
                            MainLoop();
                        }
                        while (EventQueue.Count > 0)
                        {
                            MainLoop();
                        }
                        AnalyzerChangeState(State.Stopped);
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            AnalyzerChangeState(State.Stopped);
                            var message = $"Exception in {nameof(ProgramAnalyzer)}.{nameof(StartAnalyzer)} for {ProgramName}.";
                            OnReportException?.Invoke(message, ex);
                            var alarmMessage = new AlarmMessage(AlarmLevel.Medium, AppDomain.CurrentDomain.FriendlyName, message, ex.InnerException?.Message ?? ex.Message);
                            AlarmMessageManager.RaiseAlarm(alarmMessage);
                        }
                        catch (Exception ex2)
                        {
                            OnReportException?.Invoke("Exception in Exception handling", ex2);
                        }
                    }
                }
                                                );

                analyzerThread.Priority = ThreadPriority.Highest;
                analyzerThread.Name     = nameof(analyzerThread) + "_" + ProgramName;
                analyzerThread.Start();
            }
        /// <summary>
        /// Starts the engine task.
        /// </summary>
        /// <returns></returns>
        private void StartEngine()
        {
            var engineThread = new Thread(() =>
            {
                try
                {
                    ChangeState(State.Running);
                    AddMessage("Main engine thread started.");
                    while (State == State.Running)
                    {
                        MainLoop();
                    }
                    AddMessage($"Shutting down. Running main loop until main queue is empty. {MainEventQueue.Count} events in queue.");
                    while (MainEventQueue.Count > 0)
                    {
                        MainLoop();
                    }
                    AddMessage("Main queue emptied. Shutting down analyzers");
                    foreach (var analyzer in Analyzers)
                    {
                        AddMessage($"Stopping analyzer for {analyzer.Key}");
                        analyzer.Value.StopAnalyzer();
                    }
                }
                catch (Exception ex)
                {
                    ChangeState(State.Stopped);
                    var message = $"Exception in {nameof(AnalyzerEngine)}.{nameof(StartEngine)}. Engine is down. Engine will try to restart.";
                    OnReportException?.Invoke(message, ex);
                    var alarmMessage = new AlarmMessage(AlarmLevel.Medium, AppDomain.CurrentDomain.FriendlyName, message, ex.Message);
                    AlarmMessageManager.RaiseAlarm(alarmMessage);
                }
            });

            engineThread.Priority = ThreadPriority.Highest;
            engineThread.Name     = nameof(engineThread);
            engineThread.Start();
        }
 private void HandleAnalyzerException(string message, Exception exception)
 {
     OnReportException?.Invoke(message, exception);
 }