Service1.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Cjwdev.WindowsApi;
  2. using System;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.ServiceProcess;
  7. using System.Timers;
  8. namespace MWS.Daemon
  9. {
  10. public partial class Service1 : ServiceBase
  11. {
  12. private string exePath;
  13. private int timerInterval;
  14. private string exeName;
  15. private string workPath;
  16. public Service1()
  17. {
  18. InitializeComponent();
  19. }
  20. protected override void OnStart(string[] args)
  21. {
  22. exePath = ConfigurationManager.AppSettings["exePath"].Trim();
  23. timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings["timerInterval"].Trim());
  24. exeName = Path.GetFileName(exePath).Replace(".exe", "");
  25. workPath = Path.GetDirectoryName(exePath);
  26. System.Timers.Timer timer;
  27. timer = new System.Timers.Timer();
  28. timer.Interval = timerInterval;//设置计时器事件间隔执行时间
  29. timer.Elapsed += new System.Timers.ElapsedEventHandler(Circulation);
  30. timer.Enabled = true;
  31. }
  32. private void Circulation(object sender, ElapsedEventArgs e)
  33. {
  34. Process[] myProcesses = Process.GetProcesses();
  35. foreach (Process myProcess in myProcesses)
  36. {
  37. if (myProcess.ProcessName.Contains("HDCraneCIMS.IPC.Desk"))
  38. {
  39. return;
  40. }
  41. }
  42. try
  43. {
  44. /*
  45. System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
  46. myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令
  47. myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
  48. myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
  49. myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
  50. myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
  51. myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
  52. myProcess.Start();//启动进程
  53. myProcess.StandardInput.WriteLine("shutdown -r -t 60");//执行重启计算机命令
  54. */
  55. IntPtr userTokenHandle = IntPtr.Zero;
  56. ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
  57. ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION();
  58. ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO();
  59. startInfo.cb = (uint)System.Runtime.InteropServices.Marshal.SizeOf(startInfo);
  60. ApiDefinitions.CreateProcessAsUser(
  61. userTokenHandle,
  62. exePath,
  63. "",
  64. IntPtr.Zero,
  65. IntPtr.Zero,
  66. false,
  67. 0,
  68. IntPtr.Zero,
  69. workPath,
  70. ref startInfo,
  71. out procInfo);
  72. if (userTokenHandle != IntPtr.Zero)
  73. ApiDefinitions.CloseHandle(userTokenHandle);
  74. int _currentAquariusProcessId = (int)procInfo.dwProcessId;
  75. }
  76. catch (Exception ex)
  77. {
  78. }
  79. }
  80. protected override void OnStop()
  81. {
  82. }
  83. }
  84. }