public void TestStartProcess() { Console.WriteLine(ProcessHelper.ExistProcess("Nurse.Slave") ? "有" : "无"); ProcessHelper.CloseProcess("Nurse.Slave"); ProcessHelper.StartProcess("Nurse.Slave.exe"); Console.WriteLine(ProcessHelper.ExistProcess("Nurse.Slave") ? "有" : "无"); ProcessHelper.CloseProcess("Nurse.Slave"); Console.WriteLine(ProcessHelper.ExistProcess("Nurse.Slave") ? "有" : "无"); }
protected override void OnStop() { Common.IsRun = false; if (Config.IsAlwaysRun == false) { //关闭slave进程 if (ProcessHelper.ExistProcess("Nurse.Slave")) { ProcessHelper.CloseProcess("Nurse.Slave"); } } log.Info("NurseMaster服务关闭"); }
/// <summary> /// 关闭服务 /// </summary> /// <param name="node"></param> private void CloseNode(ConfigNode node) { if (node.AppType == (int)Enums.EnumAppType.可执行程序) { if (ProcessHelper.ExistProcess(node.AppName)) { Log.Info("停掉进程:" + node.AppName); ProcessHelper.CloseProcess(node.AppName); } else { Log.Info("进程:" + node.AppName + " 本停止,不做操作"); } } else { ServiceManger sm = new ServiceManger(); if (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Running)) { //先停止服务 sm.StopService(node.AppName); Log.Info("关闭服务:" + node.AppName + " ..."); Thread.Sleep(500); if (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Stopped)) { Log.Info("关闭服务成功:" + node.AppName); } else { Log.Error("关闭服务失败:" + node.AppName); } } else { Log.Info("服务:" + node.AppName + " 本不在运行,不做操作"); } } }
private static void RecycleGuard() { string strSlaveName = "Nurse.Slave"; while (Common.IsRun) { try { if (ProcessHelper.ExistProcess(strSlaveName) == false) { int count = 0; string strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Nurse.Slave.exe"); while (ProcessHelper.StartProcess(strPath) == false) { if (count++ > 4) { CommonLog.InnerErrorLog.Error("多次启动Nurse.Slave失败,请查看安装包里是否有Nurse.Slave.exe文件"); return; } } } } catch (Exception ex) { CommonLog.InnerErrorLog.Error("Master守护slave的主程序运行出现异常!:" + ex.ToString()); } Thread.Sleep(Config.Internal_Check_Slave); } // 只有当 不执行时才会主动关闭slave 进程 if (Config.IsAlwaysRun == false) { //关闭Slave进程 ProcessHelper.CloseProcess(strSlaveName); } }
protected void CloseApp() { #region Process Handling List <string> processNames = new List <string>(); string mainPro = string.Empty; processNames.AddRange(AppParameter.AppNames); for (int i = 0; i < processNames.Count; i++) { processNames[i] = processNames[i].Substring(processNames[i].LastIndexOf('\\')).Trim('\\').Replace(".exe", ""); } mainPro = processNames.FirstOrDefault(); AppParameter.IsRunning = ProcessHelper.IsRunningProcess(mainPro); if (AppParameter.IsRunning) { MessageBox.Show("此操作需要关闭要更新的程序,请保存相关数据按确定继续", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); foreach (string item in processNames) { ProcessHelper.CloseProcess(item); } } #endregion }
/// <summary> /// 重启节点 /// </summary> /// <param name="node"></param> private void RebootNode(ConfigNode node) { if (node.AppType == (int)Enums.EnumAppType.可执行程序) { if (ProcessHelper.ExistProcess(node.AppName)) { Log.Info("先停掉进程" + node.AppName); ProcessHelper.CloseProcess(node.AppName); } if (ProcessHelper.StartProcess(node.AppPath) == false) { Log.Error("重启exe程序失败:" + node.AppName); } else { Log.Info("开启exe程序:" + node.AppPath); } } else { ServiceManger sm = new ServiceManger(); if (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Stopped)) { sm.StartService(node.AppName); Thread.Sleep(500); if (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Running)) { Log.Info("重启服务成功:" + node.AppName); } else { Log.Error("重启服务失败:" + node.AppName); } } else { for (int i = 0; i < 3; i++) { //先停止服务 sm.StopService(node.AppName); Log.Info("关闭服务:" + node.AppName + " ..."); Thread.Sleep(500); int intCount = 0; while (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Running)) { Log.Info("等待服务:" + node.AppName + "关闭..."); Thread.Sleep(500); if (intCount++ > Config.ServiceRebootReTryCount) { Log.Error("服务停止失败...."); break; } } if (intCount > Config.ServiceRebootReTryCount) { //重试关闭 continue; } Log.Info(" 开始开启服务:"); sm.StartService(node.AppName); Thread.Sleep(500); if (sm.GetServiceValue(node.AppName, "State").ToString().Equals(ServiceState.Running)) { Log.Info("重启服务成功:" + node.AppName); } else { Log.Error("重启服务失败:" + node.AppName); } } } } }
private async void RequestData(DisplayRootRegistry displayRootRegistry, DataUpdaterViewModel dataUpdater) { var net = new Net(); var dataLauncher = new LauncherInfoSerializer(); var proc = new ProcessHelper(); var settingHelp = new SettingHelper(); LauncherSettingLastSerializer lastSettingLauncher = null; try { lastSettingLauncher = settingHelp.Read <LauncherSettingLastSerializer>(CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher); dataLauncher.version = lastSettingLauncher.Info.Version; } catch (Exception ex) { if (ex is DirectoryNotFoundException || ex is FileNotFoundException) { MessageBox.Show(CommonConstant.NotFoundFileSettings); } else { MessageBox.Show(CommonConstant.ErrorReadOrWriteSettings); } proc.CloseProcess(Constant.PidLauncher); Application.Current.Shutdown(); return; } var programLauncher = await net.RequestAsync(Constant.UrlUpdaterLauncherCheckVersion, Net.Post, dataLauncher); if (programLauncher.detail != null) { MessageBox.Show(ErrorParser.Parse(programLauncher.detail)); return; } if (!programLauncher.is_updated) { var message = MessageBox.Show(Constant.DescriptionMessBoxUpdate, "Обновление", MessageBoxButton.OKCancel); if (message == MessageBoxResult.Cancel) { return; } _dataMainViewModel.TimerUpdater.Stop(); displayRootRegistry.ShowPresentation(dataUpdater); programLauncher = await net.RequestAsync <LauncherInfoSerializer>( Constant.UrlUpdaterLauncherInfo, Net.Post, null, null, null, (s, e) => { dataUpdater.ProgressValue = (e.BytesReceived * 100 / e.TotalBytesToReceive); }); if (programLauncher.detail != null) { displayRootRegistry.HidePresentation(dataUpdater); MessageBox.Show(ErrorParser.Parse(programLauncher.detail)); return; } var cryptBytesLauncher = Convert.FromBase64String(programLauncher.data); if (Hash.Sha256Bytes(cryptBytesLauncher) == programLauncher.hash) { var crypt = new Crypt(Encoding.UTF8.GetBytes(CommonConstant.Key)); crypt.RemoveAndSetIv(ref cryptBytesLauncher); var bytesLauncher = crypt.Decode(cryptBytesLauncher); proc.CloseProcess(Constant.PidLauncher); var zip = new ArchiveHelper(); var bytesLauncherSetting = zip.Extract(bytesLauncher, programLauncher.hash, Path.GetTempPath()); settingHelp.RemoveProgram(@".\", false); settingHelp.RemoveDumps <SettingLastSerializer, IRemoveData>(lastSettingLauncher.Info); settingHelp.CopyFilesProgram(Path.GetTempPath() + programLauncher.hash, ".\\"); var getLauncherSetting = settingHelp.Read <LauncherSettingSerializer>(bytesLauncherSetting, CommonConstant.KeySettings); settingHelp.RewriteLastToNewSettingsLauncher(getLauncherSetting, lastSettingLauncher, CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher); displayRootRegistry.HidePresentation(dataUpdater); try { proc.StartProcess(getLauncherSetting.Info.StartApp, new string[1] { programLauncher.hash }); } catch (System.ComponentModel.Win32Exception) { MessageBox.Show(Constant.NotFoundStartAppLauncher); } Application.Current.Shutdown(); return; } else { MessageBox.Show(Constant.NoHashEqual); } displayRootRegistry.HidePresentation(dataUpdater); _dataMainViewModel.TimerUpdater.Start(); } }
protected override async void OnStartup(StartupEventArgs e) { var proc = new ProcessHelper(); var settingHelp = new SettingHelper(); if (proc.CheckProcessByName(CommonConstant.NameAppUpdater)) { proc.CloseProcess(CommonConstant.NameAppUpdater); } var getArgs = Environment.GetCommandLineArgs(); try { if (getArgs.Length == 2) { settingHelp.RemoveProgram(CommonConstant.PathUpdater, true); settingHelp.CopyFilesProgram(Path.GetTempPath() + getArgs[1] + "\\" + CommonConstant.PathUpdater, CommonConstant.PathUpdater, true); settingHelp.RemoveProgram(Path.GetTempPath() + getArgs[1], true); } } catch (Exception) { } if (!File.Exists(CommonConstant.PathUpdater + CommonConstant.StartAppUpdater)) { MessageBox.Show(Constant.NotFoundUpdater); Shutdown(); return; } if (!File.Exists(CommonConstant.FileSettingsLauncher)) { MessageBox.Show(Constant.DescriptionMessBoxNotFoundSettings); Shutdown(); return; } var token = proc.GetPublicKeyTokenFromAssembly(Assembly.GetCallingAssembly()); if (string.IsNullOrEmpty(token)) { MessageBox.Show(Constant.CrashLauncher); Shutdown(); return; } var mut = new Mutex(true, token, out instance); if (!instance) { MessageBox.Show(Constant.AlreadyStartLauncher); Shutdown(); return; } var args = new string[1] { Process.GetCurrentProcess().Id.ToString() }; var setHelp = new SettingHelper(); var set = setHelp.Read <LauncherSettingSerializer>(CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher); Constant.PidUpdater = proc.StartProcess(CommonConstant.PathUpdater + CommonConstant.StartAppUpdater, args); base.OnStartup(e); _mainWindowViewModel = new CollectionsDataViewModel(); await DisplayRootRegistry.ShowModalPresentation(_mainWindowViewModel); Shutdown(); }
public void TestCloseProcess() { ProcessHelper.CloseProcess("Nurse.Slave"); }