/// <summary> /// The close box on the main form has been clicked. Stop the update status timer to avoid /// any confusion during the clean-up operations. If the application was communicating with /// an externally executing (UI visible) instance of CFX Manager, close the client connection, /// which will unregister with the API service and allow other clients to connect to it. If /// the application started CFX Manager in server (No UI) mode, then issue the ShutDown command /// to properly close the CFX Manager instance. Not using ShutDown to close the CFX Manager /// could lead to ghost processes on the host computer which would would prevent the further use /// of CFX Manager in any mode until host re-boot. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (m_status_update_timer != null) { m_status_update_timer.Stop(); } if (CFXManagerUtilities.CFXManagerWasStartedByApp && CFXManagerUtilities.CFXManagerIsExecuting) { if (m_ClientWrapper.ClientIsConnected()) { m_ClientWrapper.ShutDown(); } else { CFXManagerUtilities.StopCFXManager(); } } else { m_ClientWrapper.CloseClient(); } }
/// <summary> /// Initialize the example application. If a compatible version of CFX Manager is executing, establish a connection /// with it, otherwise start CFX Manager in server (no UI) mode and establish a connection with that instance. /// Subscribe to the ClientError event published by the client wrapper. Initialize local member data. Present an /// explanatory dialog and exit the application if no compatible version of CFX Manager is installed on the host machine, /// or if the client fails for any reason to establish a connection with the CFX Manager API service. /// </summary> private void InitializeMainForm() { #region local delegates implementing utilities specific to this method // Prepare to establish communications with the CFX Manager API using methods provided by the // CFXManagerUtilities class. If a compatible version of CFX Manager is installed and is already //executing, do nothing. If it is installed, but not executing, attempt to start it in server (no UI) mode. //Returns true IFF a compatible version of CFX Manager is executing at the completion of the method. Func <bool> ReadyCFXManager = () => { //If the service endpoint address is not localhost, skip this logic and return true, making the assumption //that CFX Manager is running on the remote host, since we cannot detect or start it remotely. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; ChannelEndpointElementCollection endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection; if (endpointCollection[0].Address.Host != "localhost") { LogNewLine("The service endpoint address is not localhost. Skipping CFX Manager detection logic."); return(true); } if (CFXManagerUtilities.APICompatibleCFXManagerIsInstalled) { if (!CFXManagerUtilities.CFXManagerIsExecuting) { LogNewLine("Starting CFX Manager in server mode.."); //Note that this call will block this thread for up to several seconds while CFX Manager initializes. return(CFXManagerUtilities.StartCFXManagerAsServer()); } else { LogNewLine("Detected executing instance of CFX Manager.."); } return(true); } return(false); }; //Initialize the system timer that is used to systematically poll the CFX Manager API for status updates Action <Form> StartTimer = (Form child) => { this.m_status_update_timer.Interval = c_status_update_interval; m_status_update_timer.Elapsed += new ElapsedEventHandler(UpdateTimer_Elapsed); m_status_update_timer.SynchronizingObject = child; m_status_update_timer.AutoReset = true; m_status_update_timer.Start(); }; #endregion // InitializeMainForm() Entry Point this.Text = string.Format(c_app_name); LogNewLine(string.Format("{0} is initializing..", c_app_name)); m_ClientWrapper.ClientError += HandleClientError; if (ReadyCFXManager()) { if (m_ClientWrapper.OpenClient()) { LogNewLine("Client connection established with CFX Manager API service.."); m_am_connected = true; StartTimer(this); } else { LogNewLine(string.Format("The {0} was unable to connect to the CFX Manager API service. ", c_app_name)); LogNewLine(string.Format("Review the log above for possible causes, then click the close box to exit the application. ")); m_panel_buttons.Enabled = false; } } else { string fail_msg = string.Format("CFX Manager Version {0} or higher must be installed in order to run the {1}. The application will now exit.", CFXManagerUtilities.MiniumumCFXManagerVersion, c_app_name); MessageBox.Show(string.Format("{0}", fail_msg), c_app_name, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); Close(); } }