protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         configurationForm?.Dispose();
         configurationForm = null;
     }
 }
 /// <summary>
 /// Shows service options dialog and restarts service if needed.
 /// </summary>
 private void ConfigureService()
 {
     // Check we are already configuring service.
     if (this.IsConfiguring)
     {
         return;
     }
     try
     {
         this.Timer.Stop();
         this.Tray.Icon     = this.ServiceConfiguredIcon;
         this.IsConfiguring = true;
         // Getting service options.
         APCServiceOptions remoteOptions = ((APCServiceOptions)(System.Activator.GetObject(typeof(APCServiceOptions), String.Format("tcp://{0}:{1}/APCService.rem", this.APCServiceController.MachineName, this.APCSrvOpt.MachinePort))));
         // Copy all the values to a local class.
         APCServiceOptions localOptions = new APCServiceOptions();
         remoteOptions.CopyTo(localOptions);
         // Creating window.
         this.ConfigForm = new ConfigureForm(this.APCServiceController.ServiceName, this.APCServiceController.MachineName, localOptions);
         ConfigForm.BringToFront();
         ConfigForm.TopMost = true;
         ConfigForm.TopMost = false;
         DialogResult cfr = ConfigForm.ShowDialog();
         // User wants settings to be applied.
         if ((cfr == DialogResult.OK) || (cfr == DialogResult.Yes))
         {
             // Copy all values to remote class.
             localOptions.CopyTo(remoteOptions);
             localOptions.CopyTo(this.APCSrvOpt);
             // Restarting service.
             if (cfr.Equals(System.Windows.Forms.DialogResult.Yes))
             {
                 this.CMIRestartService_Click(null, null);
             }
         }
     }
     catch (System.Runtime.Remoting.RemotingException _x)
     {
         System.Diagnostics.Debug.WriteLine(_x.ToString());
         ShowMessage(MessageType.ErrorTCPConnection, null, "Connection to " + this.APCSrvOpt.MachineName + " is established, but the service options and service provider status cannot be received");
     }
     catch (System.Exception _x)
     {
         System.Diagnostics.Debug.WriteLine(_x.ToString());
         ShowMessage(MessageType.ErrorConfiguring, null, "Unable to configure APCService now");
     }
     finally
     {
         this.LastMessageShown  = MessageType.None;
         this.LastKnownSPStatus = "UNKNOWN";
         this.IsConfiguring     = false;
         this.Timer.Start();
     }
 }
        public void ShowConfigurationForm()
        {
            if (configurationForm != null)
            {
                configurationForm.Activate();
                return;
            }

            appState.UpdateConfiguration();

            configurationForm = new ConfigureForm(appState.Configuration, machineLocator.ListMachines());

            if (configurationForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    configurationWriter.WriteConfiguration(configurationForm.UpdatedConfiguration);
                }
                catch (Exception e) when(e is InvalidOperationException || e is DirectoryNotFoundException)
                {
                    logger.LogError(e, $"Failed to write configuration {new { Error = e.Message }}");

                    MessageBox.Show(
                        $"Failed to write configuration, {e.Message}.",
                                    Properties.Resources.TrayTitle,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error
                                    );
                }

                appState.UpdateConfiguration();
            }

            configurationForm.Dispose();
            configurationForm = null;
        }