/// <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(); } }
/// <summary> /// Initializes tray icon components. /// </summary> private void InitializeTray() { this.components = new System.ComponentModel.Container(); // Icons. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(GetType()); this.ServiceIsPendingIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_INI.ico"))); this.ServiceIsRunningIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_ST.ico"))); this.ServiceIsStoppedIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_OK.ico"))); this.ServiceNotControlledIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_NON.ico"))); this.ServiceProviderIsNotOKIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_ERR.ico"))); this.ServiceConfiguredIcon = ((System.Drawing.Icon)(resources.GetObject("Icon_NON.ico"))); // Context Menu. // About. this.CMIAbout.Index = 0; this.CMIAbout.Text = "About"; this.CMIAbout.Click += new EventHandler(CMIAbout_Click); // Separator #0. this.CMISeparator0.Index = 1; this.CMISeparator0.Text = "-"; // Configure APCService. this.CMIConfigureAPCService.Index = 2; this.CMIConfigureAPCService.DefaultItem = true; this.CMIConfigureAPCService.Text = "Configure APCService"; this.CMIConfigureAPCService.Click += new EventHandler(CMIConfigureAPCService_Click); // Configure Control Tool. this.CMIConfigureControlTool.Index = 3; this.CMIConfigureControlTool.Text = "Configure Control Tool"; this.CMIConfigureControlTool.Click += new EventHandler(CMIConfigureControlTool_Click); // Separator #1. this.CMISeparator1.Index = 4; this.CMISeparator1.Text = "-"; // Start APCService. this.CMIStartService.Index = 5; this.CMIStartService.Text = "Start APCService"; this.CMIStartService.Click += new EventHandler(CMIStartService_Click); // Restart APCService. this.CMIRestartService.Index = 6; this.CMIRestartService.Text = "Restart APCService"; this.CMIRestartService.Click += new EventHandler(CMIRestartService_Click); // Stop APCService. this.CMIStopService.Index = 7; this.CMIStopService.Text = "Stop APCService"; this.CMIStopService.Click += new EventHandler(CMIStopService_Click); // Separator #2. this.CMISeparator2.Index = 8; this.CMISeparator2.Text = "-"; // Exit. this.CMIExit.Index = 9; this.CMIExit.Text = "Exit"; this.CMIExit.Click += new EventHandler(CMIExit_Click); // Adding items. this.TrayMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.CMIAbout, this.CMISeparator0, this.CMIConfigureAPCService, this.CMIConfigureControlTool, this.CMISeparator1, this.CMIStartService, this.CMIRestartService, this.CMIStopService, this.CMISeparator2, this.CMIExit }); this.TrayMenu.Popup += new EventHandler(ContextMenu_Popup); // Connect it to the tray this.Tray = new SystemTrayControl(ServiceNotControlledIcon); this.Tray.Menu = this.TrayMenu; this.Tray.Visible = false; this.Tray.LeftMouseButtonDoubleClick += new EventHandler(this.OnDoubleClick); // // Timer (disabled yet). // this.Timer = new System.Windows.Forms.Timer(this.components); this.Timer.Tick += new EventHandler(this.OnTimerTick); this.Timer.Interval = 1000; this.Timer.Enabled = false; // // Load options from the serialized file. System.IO.FileStream fs = null; try { SoapFormatter optFormatter = new SoapFormatter(); fs = new System.IO.FileStream(iniFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); this.APCSrvOpt = ((APCServiceOptions)(optFormatter.Deserialize(fs))); } catch (Exception _x) { System.Diagnostics.Debug.WriteLine(_x.ToString()); this.APCSrvOpt = new APCServiceOptions(); ShowMessage(MessageType.WarningNoOptionsFile, null, "APCService Control Tool will run with default options"); } finally { if (fs != null) { fs.Close(); } } // Seting own properties. this.Info = String.Format("Controlling \\\\{0}", this.APCSrvOpt.MachineName); // Service controller. if (this.GetServiceController()) { this.Tray.Text = "Connection established."; } else { this.Tray.Text = "Connection refused."; } this.Tray.Visible = true; // Enabling timer. this.Timer.Enabled = true; }