ProcessInfo.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.Timers;
  6. using System.ConsoleApp;
  7. using System.ConsoleApp.Commands;
  8. using System.Net.ApiService;
  9. using Keln.Progress.EventHandler;
  10. namespace Keln.Progress {
  11. public class ProcessInfo {
  12. private Process _process;
  13. private Timer _timer = new Timer(10000);
  14. private Timer _forceExitTimer = new Timer(10000);
  15. private Timer _restartTimer = new Timer(10000);
  16. private int _port;
  17. private string _uri;
  18. private eState _state = eState.NotRunning;
  19. private System.IO.FileInfo _fi;
  20. /*public delegate void StateChangeEventHandle();
  21. public delegate void StartEventHandler();
  22. public delegate void StopEventHandler();*/
  23. public event EventHandler<StateChangeEventArgs> StateChange_Event;
  24. protected virtual void OnStateChange(StateChangeEventArgs e) {
  25. EventHandler<StateChangeEventArgs> handler = StateChange_Event;
  26. if (handler != null) {
  27. handler(this, e);
  28. }
  29. }
  30. public event EventHandler<StartEventArgs> Start_Event;
  31. protected virtual void OnStart(StartEventArgs e) {
  32. EventHandler<StartEventArgs> handler = Start_Event;
  33. if (handler != null) {
  34. handler(this, e);
  35. }
  36. }
  37. public event EventHandler<StopEventArgs> Stop_Event;
  38. protected virtual void OnStop(StopEventArgs e) {
  39. EventHandler<StopEventArgs> handler = Stop_Event;
  40. if (handler != null) {
  41. handler(this, e);
  42. }
  43. }
  44. public ProcessInfo(string processPath) {
  45. this._process = new Process();
  46. this.InitProcess(processPath);
  47. this._process.Exited += _process_Exited;
  48. this._process.EnableRaisingEvents = true;
  49. this.initTimer();
  50. this._state = eState.NotRunning;
  51. }
  52. public ProcessInfo(string processPath, bool Autostart) {
  53. this._process = new Process();
  54. this.InitProcess(processPath);
  55. this._process.Exited += _process_Exited;
  56. this._process.EnableRaisingEvents = true;
  57. this.initTimer();
  58. if(Autostart && this._state != eState.Already_Running) {
  59. this.Start();
  60. }
  61. }
  62. public ProcessInfo(string processPath, EventHandler<StateChangeEventArgs> StateChangeEvent) {
  63. this._process = new Process();
  64. this.InitProcess(processPath);
  65. this._process.Exited += _process_Exited;
  66. this._process.EnableRaisingEvents = true;
  67. this.initTimer();
  68. this.StateChange_Event = StateChangeEvent;
  69. OnStateChange(new StateChangeEventArgs() { State = this._state });
  70. }
  71. public ProcessInfo(string processPath, bool Autostart, EventHandler<StateChangeEventArgs> StateChangeEvent) {
  72. this._process = new Process();
  73. this.InitProcess(processPath);
  74. this._process.Exited += _process_Exited;
  75. this._process.EnableRaisingEvents = true;
  76. this.initTimer();
  77. this.StateChange_Event = StateChangeEvent;
  78. if (Autostart && this._state != eState.Already_Running) {
  79. this.Start();
  80. } else {
  81. OnStateChange(new StateChangeEventArgs() { State = this._state });
  82. }
  83. }
  84. private void InitProcess(string path) {
  85. this._fi = new System.IO.FileInfo(path);
  86. Process[] currentRunning = Process.GetProcesses();
  87. bool bFound = false;
  88. string xpath = this._fi.FullName;
  89. string name = this._fi.Name;
  90. if (name.Contains("."))
  91. name = name.Substring(0, name.LastIndexOf("."));
  92. foreach (Process p in currentRunning) {
  93. if(p.ProcessName == name && p.MainModule.FileName == xpath) {
  94. bFound = true;
  95. this._process = Process.GetProcessById(p.Id);
  96. this._state = eState.Already_Running;
  97. break;
  98. }
  99. }
  100. if (!bFound) {
  101. this._process.StartInfo.FileName = this._fi.FullName;
  102. this._process.StartInfo.UseShellExecute = true;
  103. }
  104. }
  105. private void initTimer() {
  106. this._forceExitTimer.Elapsed += Timer_ForceExit;
  107. this._restartTimer.Elapsed += Timer_Restart;
  108. this._timer.Elapsed += Timer_CheckProcess;
  109. }
  110. private void Timer_CheckProcess(object sender, ElapsedEventArgs e) {
  111. if(this._process.HasExited && this._state == eState.Running) {
  112. this._doRestart();
  113. }
  114. }
  115. public void Start() {
  116. if (this._process != null && this._fi.Exists) {
  117. this._state = eState.Running;
  118. OnStart(new StartEventArgs());
  119. OnStateChange(new StateChangeEventArgs() { State = this._state });
  120. //BaseCommand.WriteLine("Starte Service: " + this._fi.Name, ConsoleColor.Green);
  121. this._process.Start();
  122. } else {
  123. this._state = eState.NotRunning;
  124. OnStateChange(new StateChangeEventArgs() { State = this._state });
  125. }
  126. }
  127. public void Restart() {
  128. if (this._process != null && this._fi.Exists) {
  129. this._state = eState.Restart;
  130. OnStateChange(new StateChangeEventArgs() { State = this._state });
  131. //BaseCommand.WriteLine("Retarte Service: " + this._fi.Name, ConsoleColor.Yellow);
  132. this._doRestart();
  133. }
  134. }
  135. public string ReadOutput() {
  136. if (this._process != null) {
  137. if (this._process.StandardOutput == null)
  138. return "";
  139. return this._process.StandardOutput.ReadToEnd();
  140. }
  141. return "";
  142. }
  143. private void _doRestart() {
  144. if (this._process != null && this._fi.Exists) {
  145. if (this._state == eState.Running) {
  146. // Restart application
  147. this._state = eState.Restart;
  148. OnStateChange(new StateChangeEventArgs() { State = this._state });
  149. this._restartTimer.Start();
  150. } else if (this._state == eState.Restart) {
  151. this._restartTimer.Start();
  152. }
  153. }
  154. }
  155. public void Stop() {
  156. if (this._process != null && this._fi.Exists) {
  157. this._state = eState.Stopping;
  158. OnStateChange(new StateChangeEventArgs() { State = this._state });
  159. if (!string.IsNullOrEmpty(this._uri)) {
  160. client.Get_String(this._uri + "/qqq.cgi");
  161. this._forceExitTimer.Start();
  162. } else {
  163. this._process.Kill();
  164. this._process.Close();
  165. this._state = eState.Stopped;
  166. OnStateChange(new StateChangeEventArgs() { State = this._state });
  167. }
  168. //BaseCommand.WriteLine("Stop Service: " + this._fi.Name, ConsoleColor.Red);
  169. }
  170. }
  171. private void Timer_ForceExit(object sender, ElapsedEventArgs e) {
  172. try {
  173. this._process.Kill();
  174. } catch(Exception ex) {
  175. }
  176. this._process.Close();
  177. this._forceExitTimer.Stop();
  178. this._state = eState.Stopped;
  179. OnStateChange(new StateChangeEventArgs() { State = this._state });
  180. }
  181. private void _process_Exited(object sender, EventArgs e) {
  182. //BaseCommand.WriteLine("Service was exit: " + this._fi.Name, ConsoleColor.Red);
  183. this._doRestart();
  184. }
  185. private void Timer_Restart(object sender, ElapsedEventArgs e) {
  186. this._restartTimer.Stop();
  187. //BaseCommand.WriteLine("About to restart: " + this._fi.Name, ConsoleColor.Green);
  188. this.Start();
  189. }
  190. public eState State {
  191. get { return this._state; }
  192. }
  193. public TimeSpan TotalProcessorTime {
  194. get {
  195. if (this._process != null && this._state == eState.Running)
  196. return this._process.TotalProcessorTime;
  197. return new TimeSpan(0);
  198. }
  199. }
  200. public long VirtualMemorySize64 {
  201. get {
  202. if (this._process != null && this._state == eState.Running)
  203. return this._process.VirtualMemorySize64;
  204. return 0;
  205. }
  206. }
  207. public TimeSpan UserProcessorTime {
  208. get {
  209. if (this._process != null && this._state == eState.Running)
  210. return this._process.UserProcessorTime;
  211. return new TimeSpan(0);
  212. }
  213. }
  214. public DateTime StartTime {
  215. get {
  216. if (this._process != null && this._state == eState.Running)
  217. return this._process.StartTime;
  218. return new DateTime();
  219. }
  220. }
  221. public string ProcessName {
  222. get {
  223. if (this._process != null && this._state == eState.Running)
  224. return this._process.ProcessName;
  225. return "";
  226. }
  227. }
  228. public long PrivateMemorySize64 {
  229. get {
  230. if (this._process != null && this._state == eState.Running)
  231. return this._process.PrivateMemorySize64;
  232. return 0;
  233. }
  234. }
  235. public long PeakVirtualMemorySize64 {
  236. get {
  237. if (this._process != null && this._state == eState.Running)
  238. return this._process.PeakVirtualMemorySize64;
  239. return 0;
  240. }
  241. }
  242. public long PeakWorkingSet64 {
  243. get {
  244. if (this._process != null && this._state == eState.Running)
  245. return this._process.PeakWorkingSet64;
  246. return 0;
  247. }
  248. }
  249. public long PeakPagedMemorySize64 {
  250. get {
  251. if (this._process != null && this._state == eState.Running)
  252. return this._process.PeakPagedMemorySize64;
  253. return 0;
  254. }
  255. }
  256. public long PagedSystemMemorySize64 {
  257. get {
  258. if (this._process != null && this._state == eState.Running)
  259. return this._process.PagedSystemMemorySize64;
  260. return 0;
  261. }
  262. }
  263. public long PagedMemorySize64 {
  264. get {
  265. if(this._process != null && this._state == eState.Running)
  266. return this._process.PagedMemorySize64;
  267. return 0;
  268. }
  269. }
  270. public int ExitCode {
  271. get {
  272. if (this._process != null && this._state == eState.Running)
  273. return this._process.ExitCode;
  274. return -1;
  275. }
  276. }
  277. }
  278. }