/// <summary> /// Starts the monitor thread, which is waiting for a new Alarm. /// </summary> /// <exception cref="System.InvalidOperationException">Service start failed because of one of the following reasons. - There are no running alarm sources. - A general exception occurred.</exception> public void Start() { if (IsStarted) { return; } InitializeOperationStore(); InitializeRoutePlanProvider(); InitializeAlarmSources(); _jobManager = new JobManager(); _jobManager.Initialize(); // Initialize each alarm source and register event handler int iInitializedSources = 0; foreach (IAlarmSource alarmSource in _alarmSources) { try { alarmSource.Initialize(); alarmSource.NewAlarm += AlarmSource_NewAlarm; // Create new thread Thread ast = new Thread(AlarmSourceThreadWrapper); // Use lower priority since we have many threads ast.Priority = ThreadPriority.BelowNormal; ast.Name = string.Format("AlarmWorkflow.Engine.Thread.$" + alarmSource.GetType().Name); // Start the thread _alarmSourcesThreads.Add(alarmSource, ast); ast.Start(alarmSource); iInitializedSources++; } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Warning, this, "Error initializing alarm source '{0}'. It will not run.", alarmSource.GetType().FullName); Logger.Instance.LogException(this, ex); } } if (iInitializedSources > 0) { Logger.Instance.LogFormat(LogType.Info, this, "Started Service"); IsStarted = true; return; } else { // Having no alarm sources is very critical - throw exception Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.ServiceStartFailedNoAlarmSourceException); throw new InvalidOperationException(Properties.Resources.ServiceStartFailedNoAlarmSourceException); } }
/// <summary> /// Stops the engine and disposes all alarm sources and other plugins. /// </summary> public void Stop() { if (!IsStarted) { return; } Logger.Instance.LogFormat(LogType.Info, this, "Stopping Service"); // Dispose and kill all threads foreach (IAlarmSource alarmSource in _alarmSources) { try { // Unregister from event and dispose source alarmSource.NewAlarm -= AlarmSource_NewAlarm; alarmSource.Dispose(); // Stop and remove the thread, if existing if (_alarmSourcesThreads.ContainsKey(alarmSource)) { Thread ast = _alarmSourcesThreads[alarmSource]; // Abort and ignore exception ast.Abort(); _alarmSourcesThreads.Remove(alarmSource); } } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Warning, this, "Error disposing alarm source '{0}'.", alarmSource.GetType().FullName); Logger.Instance.LogException(this, ex); } } _jobManager.Dispose(); _jobManager = null; // Dispose operation store _operationStore = null; // Dispose route plan provider _routePlanProvider = null; Logger.Instance.LogFormat(LogType.Info, this, "Stopped Service"); IsStarted = false; }