Пример #1
0
 private void ServiceStatusUpdate(object sender, DoWorkEventArgs e)
 {
     try
     {
         service_status_confirmation.CancelAsync();
     }
     catch (Exception ex)
     {
     }
     service_status_confirmation.RunWorkerAsync();
     System.ServiceProcess.ServiceControllerStatus aim = (ServiceControllerStatus)e.Argument;
     try
     {
         serviceController1.WaitForStatus(aim, TimeSpan.FromSeconds(30));
         service_status_confirmation.CancelAsync();
         service_status_progress_updater.ReportProgress(100);
     }
     catch (Exception ez)
     {
         service_status_confirmation.CancelAsync();
         service_status_progress_updater.ReportProgress(0);
     }
 }
        /// <summary>
        /// Tests configuration is valid (connects to service and gets its options).
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event parameters.</param>
        private void TestOptions(object sender, System.EventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                // Close all previously allocated Service Controllers
                if (this.APCServiceController != null)
                {
                    this.APCServiceController.Close();
                    this.APCServiceController = null;
                }

                // Trying to fing IP address of target machine...
                this.opt.MachineName = System.Net.Dns.GetHostEntry(this.TargetMachineNameTextBox.Text).AddressList[0].ToString();
                this.TargetMachineNameTextBox.Text = this.opt.MachineName;
                this.TargetMachineNameTextBox.Update();
                // Getting list of services for specified machine.
                System.ServiceProcess.ServiceController [] services = System.ServiceProcess.ServiceController.GetServices(this.opt.MachineName);
                // Looking for "APCService" service.
                foreach (System.ServiceProcess.ServiceController service in services)
                {
                    if (service.ServiceName.Equals("APCService"))
                    {
                        this.APCServiceController = service;
                        break;
                    }
                }
                if (this.APCServiceController == null)
                {
                    MessageBox.Show("APCService is not found...", "APCService Error", MessageBox.Type.Error);
                    return;
                }
                this.Text = "Service found, waiting for reply...";

                System.DateTime DueTime = DateTime.Now.AddSeconds(10);
                this.APCServiceController.Refresh();
                ServiceControllerStatus _stat = this.APCServiceController.Status;
                while ((_stat != ServiceControllerStatus.Stopped) &&
                       (_stat != ServiceControllerStatus.Running) &&
                       (_stat != ServiceControllerStatus.Paused))
                {
                    // It does something... Lets wait action to be done.
                    if (DateTime.Now > DueTime)
                    {
                        MessageBox.Show("APCService is not responding...", "APCService Error", MessageBox.Type.Error);
                        return;
                    }
                    System.Threading.Thread.Sleep(1000);
                    this.APCServiceController.Refresh();
                    _stat = this.APCServiceController.Status;
                }

                // Service now is able to execute the appropriate command.
                this.PreviousState = _stat;
                switch (_stat)
                {
                case System.ServiceProcess.ServiceControllerStatus.Paused:
                    this.APCServiceController.Continue();
                    break;

                case System.ServiceProcess.ServiceControllerStatus.Stopped:
                    this.APCServiceController.Start();
                    break;
                }

                // Waiting for service to run...
                DueTime   = DateTime.Now.AddSeconds(10);
                this.Text = "Waiting for service to run...";
                this.APCServiceController.Refresh();
                while (this.APCServiceController.Status != ServiceControllerStatus.Running)
                {
                    // It is running... Lets wait...
                    if (DateTime.Now > DueTime)
                    {
                        MessageBox.Show("APCService is not responding to start command ...", "APCService Error", MessageBox.Type.Error);
                        return;
                    }
                    System.Threading.Thread.Sleep(500);
                    this.APCServiceController.Refresh();
                }
                this.Text = "Getting service options...";
                try
                {
                    // Use this convoluted mechanism due to new security restrictions in .NET 1.1
                    BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
                    BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
                    serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

                    IDictionary props = new Hashtable();
                    props["port"]                = 0;                                // Dynamically assigned port
                    props["name"]                = System.Guid.NewGuid().ToString(); // Some random name
                    props["typeFilterLevel"]     = TypeFilterLevel.Full;
                    props["exclusiveAddressUse"] = false;
                    RemoteChannel                = new TcpChannel(props, clientProvider, serverProvider);
                    ChannelServices.RegisterChannel(RemoteChannel, false);

                    APCService.APCServiceOptions aAPCServOpt = ((APCService.APCServiceOptions)(System.Activator.GetObject(typeof(APCService.APCServiceOptions), String.Format("tcp://{0}:{1}/APCService.rem", this.APCServiceController.MachineName, this.opt.MachinePort))));
                    // Copy all the values to a local class.
                    aAPCServOpt.CopyTo(this.opt);
                    // Let user to know all is OK, configuration is valid.
                    string msg = "APCServiceControl configuration has been tested." + Environment.NewLine + Environment.NewLine + "APCService found on \"" + this.opt.MachineName + "\"." + Environment.NewLine + "APCService is running now." + Environment.NewLine + Environment.NewLine + this.opt.ToString();
                    this.Text = "See service options in message box window";
                    MessageBox.Show(msg, "Congratulations!!!", MessageBox.Type.Information);
                    // Test is successfully finished, configuration now can be applied.
                    this.TargetMachineNameTextBox.Text = this.opt.MachineName;
                }
                catch (Exception x)
                {
                    System.Diagnostics.Debug.WriteLine(x.ToString());
                    MessageBox.Show("APCService is running, but configuration cannot be accessed ..." + Environment.NewLine + "Please, check the port for connection or use following information:" + Environment.NewLine + x.ToString(), "APCService Error", MessageBox.Type.Error);
                    return;
                }
                // Everything worked out perfectly well - enable configuration buttons
                this.Text = "APCServiceControl configuration manager";
                this.ApplyConfigurationButton.Enabled = true;
                this.AutoStartCheckBox.Enabled        = true;
                this.StartCheckBox.Enabled            = true;
            }
            catch (Exception x)
            {
                System.Diagnostics.Debug.WriteLine(x.ToString());
                MessageBox.Show("APCService is running, but connection couldn't be established..." + Environment.NewLine + "Please, check the port for connection or use following information:" + Environment.NewLine + x.ToString(), "APCService Error", MessageBox.Type.Error);
            }
            finally
            {
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }
Пример #3
0
        // dati statici
        public static bool DoesServiceExist(string serviceName, out string DisplayName, out System.ServiceProcess.ServiceControllerStatus Stato)
        {
            ServiceController[] services = ServiceController.GetServices();
            Stato       = ServiceControllerStatus.Running;
            DisplayName = "";
            var service = services.FirstOrDefault(s => s.ServiceName.ToLower() == serviceName.ToLower());

            if (service != null)
            {
                Stato       = service.Status;
                DisplayName = service.DisplayName;
            }

            return(service != null);
        }