Beispiel #1
0
        private void LoadGridByServicesInfo()
        {
            var services       = ServicesHelper.GetAllServices();
            var servicesSource = services.ToDataTable();

            SetColumnsToReadOnly(servicesSource);

            servicesSource.Columns.Add("Selected", typeof(bool)).SetOrdinal(0);

            dgvServices.DataSource = servicesSource;
        }
        public static void NotifyStoppedCoveredServices()
        {
            lock (SyncObj)
            {
                var allServices = ServicesHelper.GetAllServices(); // all services
                try
                {
                    foreach (var keepService in NewSetting.CoveredServices)
                    {
                        // find any new state of covered services
                        var serviceNewStatus =
                            allServices.FirstOrDefault(x => x.Name.Equals(keepService.Service.Name, StringComparison.CurrentCultureIgnoreCase))?.Status;

                        // find any old state of covered services
                        var serviceOldStatus =
                            OldSetting.CoveredServices.FirstOrDefault(s => s.Service.Name.Equals(keepService.Service.Name, StringComparison.CurrentCultureIgnoreCase))?.Service.Status;

                        // set new state to new setting service
                        keepService.Service.Status = serviceNewStatus ?? ServiceControllerStatus.Stopped;

                        // the service stopped just now!!!
                        ServiceControllerStatusChanging newStatus;
                        Enum.TryParse(serviceNewStatus.ToString(), true, out newStatus);

                        // if status changed and identify status changing...
                        if (serviceNewStatus != serviceOldStatus)
                        {
                            if (NewSetting.NotifyJustStatusChangingTo.HasFlag(newStatus))
                            {
                                OnServiceStatusChanged(new ServiceNotifyEventArgs(keepService, serviceNewStatus ?? ServiceControllerStatus.Stopped));
                            }
                        }
                    }
                }
                catch (Exception exp)
                {
                    WindowsEventLog.WriteErrorLog($"Exception in ServiceLifeControllerService: {exp.Message}");
                }
                finally
                {
                    if (allServices != null)
                    {
                        GC.SuppressFinalize(allServices);
                    }

                    OldSetting = NewSetting;
                }
            }
        }