Ejemplo n.º 1
0
        /// <summary>
        /// 杀进程
        /// </summary>
        /// <param name="AStrProcessId"></param>
        private static OperationReturn KillProcess(string AStrProcessId)
        {
            OperationReturn optReturn = new OperationReturn();

            optReturn.Result = true;
            optReturn.Code   = Defines.RET_SUCCESS;

            try
            {
                ManagementObjectCollection ObjectCollection;
                ObjectCollection = GetServiceCollection("Select * from win32_process where ProcessID = '" + AStrProcessId + "'");
                foreach (ManagementObject SingleCollenction in ObjectCollection)
                {
                    SingleCollenction.InvokeMethod("Terminate", null);
                }
                return(optReturn);
            }
            catch (Exception ex)
            {
                optReturn.Result  = false;
                optReturn.Code    = ConstDefines.Kill_Process_Exception;
                optReturn.Message = ex.Message;
                return(optReturn);
            }
        }
Ejemplo n.º 2
0
 private void KillProcess(string AStrProcessId)
 {
     try
     {
         ManagementObjectCollection ObjectCollection;
         ObjectCollection = GetServiceCollection("Select * from win32_process where ProcessID = '" + AStrProcessId + "'");
         foreach (ManagementObject SingleCollenction in ObjectCollection)
         {
             SingleCollenction.InvokeMethod("Terminate", null);
         }
     }
     catch (Exception ex)
     {
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 杀进程
 /// </summary>
 /// <param name="AStrProcessId"></param>
 private static void KillProcess(string AStrProcessId)
 {
     try
     {
         ManagementObjectCollection ObjectCollection;
         ObjectCollection = GetServiceCollection("Select * from win32_process where ProcessID = '" + AStrProcessId + "'");
         foreach (ManagementObject SingleCollenction in ObjectCollection)
         {
             SingleCollenction.InvokeMethod("Terminate", null);
         }
     }
     catch (Exception ex)
     {
         UMPService00.WriteLog(LogMode.Error, "KillProcess error : " + ex.Message);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="strServiceName"></param>
        /// <returns></returns>
        public static OperationReturn StartServiceByNmae(string strServiceName)
        {
            OperationReturn optReturn = new OperationReturn();

            optReturn.Result = true;
            optReturn.Code   = Defines.RET_SUCCESS;

            try
            {
                InvokeMethodOptions  OperationMethodOptions = new InvokeMethodOptions(null, new TimeSpan(0, 10, 0));
                ManagementBaseObject LOutStopService;

                ManagementObjectCollection ObjectCollection;
                ObjectCollection = GetServiceCollection(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", strServiceName));
                int    i               = ObjectCollection.Count;
                string LStrProcessId   = string.Empty;
                string LStrDisplayName = string.Empty;
                int    LIntReturnValue = -1;

                foreach (ManagementObject SingleCollenction in ObjectCollection)
                {
                    for (int iStartCount = 0; iStartCount < 30; iStartCount++)
                    {
                        LOutStopService = SingleCollenction.InvokeMethod("StartService", null, OperationMethodOptions);
                        LIntReturnValue = int.Parse(LOutStopService["ReturnValue"].ToString());
                        if (LIntReturnValue == 0)
                        {
                            return(optReturn);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                optReturn.Result  = false;
                optReturn.Code    = ConstDefines.Start_Service_Exception;
                optReturn.Message = ex.Message;
                return(optReturn);
            }
            return(optReturn);
        }
Ejemplo n.º 5
0
        public static OperationReturn StopServiceByName(string strServiceName)
        {
            OperationReturn optReturn = new OperationReturn();

            optReturn.Result = true;
            optReturn.Code   = Defines.RET_SUCCESS;

            try
            {
                InvokeMethodOptions  OperationMethodOptions = new InvokeMethodOptions(null, new TimeSpan(0, 10, 0));
                ManagementBaseObject LOutStopService;

                ManagementObjectCollection ObjectCollection;
                ObjectCollection = GetServiceCollection(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", strServiceName));
                int    i               = ObjectCollection.Count;
                string LStrProcessId   = string.Empty;
                string LStrDisplayName = string.Empty;
                int    LIntReturnValue = -1;
                string strStatus       = string.Empty;

                foreach (ManagementObject SingleCollenction in ObjectCollection)
                {
                    LStrProcessId   = SingleCollenction["ProcessId"].ToString();
                    LStrDisplayName = SingleCollenction["DisplayName"].ToString();
                    strStatus       = SingleCollenction["State"].ToString();
                    if (strStatus == "Stopped")
                    {
                        return(optReturn);
                    }
                    LOutStopService = SingleCollenction.InvokeMethod("StopService", null, OperationMethodOptions);
                    LIntReturnValue = int.Parse(LOutStopService["ReturnValue"].ToString());
                    if (LIntReturnValue != 0)
                    {
                        int    LIntWaitCount            = 0;
                        string LStrServiceCurrentStatus = string.Empty;
                        optReturn = GetComputerServiceStatus(strServiceName);
                        if (!optReturn.Result)
                        {
                            return(optReturn);
                        }
                        LStrServiceCurrentStatus = optReturn.Data as string;
                        while (LStrServiceCurrentStatus != "0" && LIntWaitCount < 30)
                        {
                            System.Threading.Thread.Sleep(1000);
                            LIntWaitCount += 1;
                            optReturn      = GetComputerServiceStatus(strServiceName);
                            if (!optReturn.Result)
                            {
                                return(optReturn);
                            }
                            LStrServiceCurrentStatus = optReturn.Data as string;
                            if (LStrServiceCurrentStatus == "0")
                            {
                                break;
                            }
                        }
                        if (LStrServiceCurrentStatus == "0")
                        {
                            LIntReturnValue = 0;
                        }
                        else
                        {
                            KillProcess(LStrProcessId); LIntReturnValue = 0;
                        }
                    }
                    //判断服务状态 直到变成Stopped为止 30s不停止 杀掉进程
                    optReturn = GetComputerServiceStatus(strServiceName);
                    if (!optReturn.Result)
                    {
                        return(optReturn);
                    }
                    int status = (optReturn.Data as ServiceEnty).ServiceStatus;
                    int iCount = 0;
                    while (status != 1 && iCount < 30)
                    {
                        System.Threading.Thread.Sleep(1000);
                        optReturn = GetComputerServiceStatus(strServiceName);
                        if (!optReturn.Result)
                        {
                            return(optReturn);
                        }
                        status = (optReturn.Data as ServiceEnty).ServiceStatus;
                        iCount++;
                        if (status == 1)
                        {
                            break;
                        }
                    }
                    if (status != 1)
                    {
                        KillProcess(LStrProcessId);
                    }
                }
                //保险起见 再杀一次进程
                if (!string.IsNullOrEmpty(LStrProcessId))
                {
                    KillProcess(LStrProcessId);
                }
                return(optReturn);
            }
            catch (Exception ex)
            {
                optReturn.Result  = false;
                optReturn.Code    = ConstDefines.Stop_Service_Exception;
                optReturn.Message = ex.Message;
                return(optReturn);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 重启服务
        /// </summary>
        /// <param name="strServiceName"></param>
        public static bool RestartService(string strServiceName)
        {
            try
            {
                InvokeMethodOptions  OperationMethodOptions = new InvokeMethodOptions(null, new TimeSpan(0, 10, 0));
                ManagementBaseObject LOutStopService;

                ManagementObjectCollection ObjectCollection;
                ObjectCollection = GetServiceCollection(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", strServiceName));
                int    i               = ObjectCollection.Count;
                string LStrProcessId   = string.Empty;
                string LStrDisplayName = string.Empty;
                int    LIntReturnValue = -1;

                foreach (ManagementObject SingleCollenction in ObjectCollection)
                {
                    LStrProcessId   = SingleCollenction["ProcessId"].ToString();
                    LStrDisplayName = SingleCollenction["DisplayName"].ToString();
                    LOutStopService = SingleCollenction.InvokeMethod("StopService", null, OperationMethodOptions);
                    LIntReturnValue = int.Parse(LOutStopService["ReturnValue"].ToString());
                    UMPService00.WriteLog("StopService ReturnValue = " + LIntReturnValue.ToString());
                    if (LIntReturnValue != 0)
                    {
                        int    LIntWaitCount            = 0;
                        string LStrServiceCurrentStatus = GetComputerServiceStatus(strServiceName);
                        if (string.IsNullOrEmpty(LStrServiceCurrentStatus))
                        {
                            UMPService00.WriteLog("GetComputerServiceStatus null");
                            return(false);
                        }
                        while (LStrServiceCurrentStatus != "0" && LIntWaitCount < 30)
                        {
                            System.Threading.Thread.Sleep(2000);
                            LIntWaitCount           += 1;
                            LStrServiceCurrentStatus = GetComputerServiceStatus(strServiceName);
                            UMPService00.WriteLog("GetComputerServiceStatus LStrServiceCurrentStatus = " + LStrServiceCurrentStatus.ToString());
                            if (string.IsNullOrEmpty(LStrServiceCurrentStatus))
                            {
                                UMPService00.WriteLog("GetComputerServiceStatus LStrServiceCurrentStatus is null ,return false");
                                return(false);
                            }
                        }
                        if (LStrServiceCurrentStatus == "0")
                        {
                            LIntReturnValue = 0;
                        }
                        else
                        {
                            KillProcess(LStrProcessId); LIntReturnValue = 0;
                        }

                        //for (int iStartCount = 0; iStartCount < 30; iStartCount++)
                        //{
                        //    Thread.Sleep(2000);
                        //    try
                        //    {
                        //        UMPService00.IEventLog.WriteEntry("start service begin " + iStartCount.ToString());
                        //        LOutStopService = SingleCollenction.InvokeMethod("StartService", null, OperationMethodOptions);
                        //        LIntReturnValue = int.Parse(LOutStopService["ReturnValue"].ToString());
                        //        UMPService00.IEventLog.WriteEntry("Restart service " + strServiceName + ", " + iStartCount.ToString() + ", result = " + LIntReturnValue.ToString(), EventLogEntryType.Warning);
                        //        if (LIntReturnValue == 0)
                        //        {
                        //            return true;
                        //        }
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        UMPService00.IEventLog.WriteEntry("start service exception : " + ex.Message);
                        //    }
                        //}
                    }
                    UMPService00.WriteLog("stop server success", EventLogEntryType.Information);
                    for (int iStartCount = 0; iStartCount < 30; iStartCount++)
                    {
                        Thread.Sleep(2000);
                        try
                        {
                            UMPService00.WriteLog("start service begin " + iStartCount.ToString());
                            LOutStopService = SingleCollenction.InvokeMethod("StartService", null, OperationMethodOptions);
                            LIntReturnValue = int.Parse(LOutStopService["ReturnValue"].ToString());
                            UMPService00.WriteLog(LogMode.Error, "Restart service " + strServiceName + ", " + iStartCount.ToString() + ", result = " + LIntReturnValue.ToString());
                            if (LIntReturnValue == 0)
                            {
                                return(true);
                            }
                        }
                        catch (Exception ex)
                        {
                            UMPService00.WriteLog("start service exception : " + ex.Message);
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                UMPService00.WriteLog(EventLogEntryType.Error, "Restart service " + strServiceName + " error : " + ex.Message);
                return(false);
            }
        }