/// <summary> /// 打开应用程序 /// </summary> /// <param name="appPath"></param> public static void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) ApiDefinitions.CloseHandle(userTokenHandle); int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { LogHelpr.Error("打开程序时出错:" + ex.ToString()); } }
protected override void OnStop() { if (_timer != null) { _timer.Stop(); _timer.Dispose(); LogHelpr.Info("守护服务停止"); } }
/// <summary> /// 监测应用程序运行状态 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Timer_AppSmart(object sender, System.Timers.ElapsedEventArgs e) { if (Process.GetProcessesByName(toWatchAppName).ToList().Count < 1) { LogHelpr.Info("监测到应用程序已经关闭......"); LogHelpr.Info("正在重新启动应用程序......"); AppStart(@FilePath+toWatchAppName + ".exe"); } }
protected override void OnStart(string[] args) { //服务启动时开启定时器 _timer = new System.Timers.Timer { Interval = _timerInterval, Enabled = true, AutoReset = true }; _timer.Elapsed += Timer_Elapsed; _timer.Elapsed += Timer_AppSmart; LogHelpr.Info("守护服务开启"); }
/// <summary> /// 启动服务 /// </summary> /// <param name="serviceName">要启动的服务名称</param> private void StartService(string serviceName) { try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName.Trim() == serviceName.Trim()) { service.Start(); //直到服务启动 service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30)); LogHelpr.Info(string.Format("启动服务:{0}", serviceName)); } } } catch (Exception ex) { LogHelpr.Error(ex.ToString()); } }
private bool CheckSericeStart(string serviceName) { bool result = true; try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName.Trim() == serviceName.Trim()) { if ((service.Status == ServiceControllerStatus.Stopped) || (service.Status == ServiceControllerStatus.StopPending)) { result = false; } } } } catch (Exception ex) { LogHelpr.Error(ex.ToString()); } return result; }