/// <summary> /// Stops this instance of the RequestController if it has not already been stopped. /// </summary> /// <remarks>Stops this instance of the RequestController if it is not already stopped by calling Stop() on each of the RequestManagers being controlled.</remarks> public void Stop() { // If we're running, stop each of the controlled RequestManagers if (isRunning) { try { // Stop each of the request managers foreach (RequestManager requestManager in requestManagers) { // Stop the request manager requestManager.Stop(); } // Log that we've stopped LogEntry logEntry = new LogEntry( "The Request Controller has stopped.", "Request Management", 0, 0, TraceEventType.Stop, "Request Management", null); // Write to the log Logger.Write(logEntry); } catch (Exception ex) { // Store the last error lastError = ex; // Log that we've failed if (ExceptionPolicy.HandleException(ex, "Request Management")) { // Rethrow the exception throw; } } finally { // Clear existing config requestManagers.Clear(); customSettings.Clear(); validationErrors.Clear(); // We're not running isRunning = false; // If we have a delegate for status events, call it if (null != StatusChanged) { // Call the delegate asynchronously StatusChanged.BeginInvoke(RequestControllerStatus.Stopped, lastError, null, null); } } } }
private void RunThread(object obj) { IsRunning = true; StatusChanged?.BeginInvoke(this, new EventArgs(), null, null); try { Run(); } finally { IsRunning = false; StatusChanged?.BeginInvoke(this, new EventArgs(), null, null); } }
/// <summary> /// Starts this instance of the RequestController if it is not already running. /// </summary> /// <remarks>Starts this instance of the RequestController if it is not already running by calling Start() /// on each of the RequestManagers being controlled.</remarks> public void Start() { // Enter the controller Monitor.Enter(this); // Make sure we're not already running if (!isRunning) { try { // We're running, do this now so we can tidy up if it goes wrong isRunning = true; // Get the path the the executing assembly string assemblyPath = Assembly.GetExecutingAssembly().Location; // Load the request manager configuration (Discovery.RequestManager.Config) LoadConfiguration(assemblyPath.Replace(".dll", ".xml")); // See if we're tracing if (this.Trace) { // Log debug information (using HTML file format) Logger.Write(new LogEntry( "Loading configuration file <b>" + assemblyPath.Replace(".dll", ".xml") + "</b>", "Request Management Trace", 0, 0, TraceEventType.Information, "Request Management Trace", null)); } // Start each of the request managers loaded from the config foreach (RequestManager requestManager in requestManagers) { // Start the request manager requestManager.Start(); } // If we have a delegate for status events, call it if (null != StatusChanged) { // Call the delegate asynchronously StatusChanged.BeginInvoke(RequestControllerStatus.Started, lastError, null, null); } // Log that we've started ok LogEntry logEntry = new LogEntry( "The Request Controller has started.", "Request Management", 0, 0, TraceEventType.Start, "Request Management", null); // Write to the log Logger.Write(logEntry); } catch (Exception ex) { // Store the exception lastError = ex; // Tidy up this.Stop(); // Log that we failed to start if (ExceptionPolicy.HandleException(ex, "Request Management")) { // Rethrow the exception throw; } } finally { // Leave the controller Monitor.Exit(this); } } }