Esempio n. 1
0
 public void RefreshServiceStatus()
 {
     serviceController.Refresh();
     if (ServiceError)
     {
         smartLabelServiceStatus.Text      = "Service not found!";
         smartLabelServiceStatus.ForeColor = Color.FromArgb(225, 50, 50);
         return;
     }
     try {
         if (serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running && !IsServiceRunning)
         {
             IsServiceRunning                  = true;
             pictureBoxStartService.Image      = global::Cyberarms.IntrusionDetection.Admin.Properties.Resources.service_controller_start_deactivated;
             pictureBoxStartService.Enabled    = false;
             pictureBoxStopService.Image       = global::Cyberarms.IntrusionDetection.Admin.Properties.Resources.service_controller_stop;
             pictureBoxStopService.Enabled     = true;
             smartLabelServiceStatus.Text      = "Service is running";
             smartLabelServiceStatus.ForeColor = Color.FromArgb(0, 159, 227);
         }
         else if (serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped && IsServiceRunning)
         {
             IsServiceRunning                  = false;
             pictureBoxStartService.Image      = global::Cyberarms.IntrusionDetection.Admin.Properties.Resources.service_controller_start;
             pictureBoxStartService.Enabled    = true;
             pictureBoxStopService.Image       = global::Cyberarms.IntrusionDetection.Admin.Properties.Resources.service_controller_stop_deactivated;
             pictureBoxStopService.Enabled     = false;
             smartLabelServiceStatus.Text      = "Service is stopped";
             smartLabelServiceStatus.ForeColor = Color.FromArgb(225, 50, 50);
         }
     } catch {
         ServiceError = true;
     }
 }
        internal static bool EnsureSqlExpressServiceIsRunning()
        {
            System.ServiceProcess.ServiceController cc = new System.ServiceProcess.ServiceController("MSSQL$SQLEXPRESS");
            bool result = true;

            if (cc == null)
            {
                throw new Exception("SQLExpress Windows Service not found. Please install SQL Server Express.");
            }
            if (cc.Status != System.ServiceProcess.ServiceControllerStatus.Running)
            {
                if (MessageBox.Show("SQLExpress is not running. Start?", "SQL Express", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cc.Start();
                    cc.Refresh();
                    if (cc.Status != System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        MessageBox.Show(@"There was a problem starting the SQLExpress Windows Service. Please start it using 'Control Panel\Administration Tools\Services' .", "SQL Express", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        result = false;
                    }
                }
                else
                {
                    result = false;
                }
            }
            return(result);
        }
Esempio n. 3
0
 /// <summary>
 /// 启动服务
 /// </summary>
 /// <param name="serviceName"></param>
 public static void StartService(string serviceName)
 {
     if (IsServiceExisted(serviceName))
     {
         System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
         if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running &&
             service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
         {
             service.Start();
             for (int i = 0; i < 60; i++)
             {
                 service.Refresh();
                 System.Threading.Thread.Sleep(1000);
                 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                 {
                     break;
                 }
                 if (i == 59)
                 {
                     throw new Exception("Start Service Error:" + serviceName);
                 }
             }
         }
     }
 }
Esempio n. 4
0
        private async Task WaitForStatusChange(System.ServiceProcess.ServiceController serviceController, System.ServiceProcess.ServiceControllerStatus NewStatus)
        {
            int count = 0;

            while (serviceController.Status != NewStatus && count < 30)
            {
                await Task.Delay(1000);

                serviceController.Refresh();
                count++;
                if (NewStatus == System.ServiceProcess.ServiceControllerStatus.Running && serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    break;
                }
            }
            if (serviceController.Status != NewStatus)
            {
                throw new Exception("Failed to change status of service. Current status: " + serviceController.Status + " Desired status: " + NewStatus);
            }
        }
Esempio n. 5
0
 private void llblStartLocalService_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     try
     {
         System.ServiceProcess.ServiceController qsvrc = new System.ServiceProcess.ServiceController("QuickMon 4 Service");
         if (qsvrc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
         {
             qsvrc.Start();
             qsvrc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
             qsvrc.Refresh();
             if (qsvrc.Status == System.ServiceProcess.ServiceControllerStatus.Running)
             {
                 MessageBox.Show("Service started", "Service", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 llblStartLocalService.Visible = false;
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Esempio n. 6
0
 private bool StopSystemService(string service)
 {
     if (!SystemServiceExists(service))
         return false;
     try
     {
         System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
         //sc.MachineName = "localhost";
         sc.ServiceName = service;
         if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Running)
         {
             sc.Stop();
             for (int i = 0; i < 60; i++)
             {
                 SetProgress(0, 60, i);
                 sc.Refresh();
                 System.Threading.Thread.Sleep(1000);
                 if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                 {
                     SetProgress(0, 60, 60);
                     return true;
                 }
             }
             SetProgress(0, 60, 60);
             return false;
         }
         return true;
     }
     catch (Exception e)
     {
         ShowError(e.StackTrace.ToString());
         return false;
     }
 }
Esempio n. 7
0
 private string GetSystemServiceStatus(string service)
 {
     try
     {
         //foreach (System.ServiceProcess.ServiceController sc in System.ServiceProcess.ServiceController.GetServices())
         //{
         //    if (sc.ServiceName.ToUpper().CompareTo(service.ToUpper()) == 0)
         //    {
         //        return sc.Status.ToString();
         //    }
         //}
         System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
         //sc.MachineName = "localhost";
         sc.ServiceName = service;
         sc.Refresh();
         switch (sc.Status)
         {
             case System.ServiceProcess.ServiceControllerStatus.StartPending:
                 return "服务正在启动!";
             case System.ServiceProcess.ServiceControllerStatus.ContinuePending:
                 return "服务即将继续!";
             case System.ServiceProcess.ServiceControllerStatus.Paused:
                 return "服务已暂停!";
             case System.ServiceProcess.ServiceControllerStatus.PausePending:
                 return "服务即将暂停!";
             case System.ServiceProcess.ServiceControllerStatus.Running:
                 return "服务正在运行!";
             case System.ServiceProcess.ServiceControllerStatus.Stopped:
                 return "服务未运行!";
             case System.ServiceProcess.ServiceControllerStatus.StopPending:
                 return "服务正在停止!";
             default :
                 return "服务状态未知!";
         }
     }
     catch (Exception e)
     {
         //ShowError(e.StackTrace.ToString());
         return "Error:" + e.Message;
     }
 }
        internal static bool EnsureSqlExpressServiceIsRunning()
        {
            System.ServiceProcess.ServiceController cc = new System.ServiceProcess.ServiceController("MSSQL$SQLEXPRESS");
            bool result = true;

            if (cc == null)
            {
                throw new Exception("SQLExpress Windows Service not found. Please install SQL Server Express.");
            }
            if (cc.Status != System.ServiceProcess.ServiceControllerStatus.Running)
            {
                if (MessageBox.Show("SQLExpress is not running. Start?", "SQL Express", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cc.Start();
                    cc.Refresh();
                    if (cc.Status != System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        MessageBox.Show(@"There was a problem starting the SQLExpress Windows Service. Please start it using 'Control Panel\Administration Tools\Services' .", "SQL Express", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        result = false;
                    }
                }
                else
                {
                    result = false;
                }
            }
            return result;
        }