| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Timers;
- using System.ConsoleApp;
- using System.ConsoleApp.Commands;
- using System.Net.ApiService;
- using Keln.Progress.EventHandler;
- namespace Keln.Progress {
- public class ProcessInfo {
-
- private Process _process;
- private Timer _timer = new Timer(10000);
- private Timer _forceExitTimer = new Timer(10000);
- private Timer _restartTimer = new Timer(10000);
- private int _port;
- private string _uri;
- private eState _state = eState.NotRunning;
- private System.IO.FileInfo _fi;
- /*public delegate void StateChangeEventHandle();
- public delegate void StartEventHandler();
- public delegate void StopEventHandler();*/
- public event EventHandler<StateChangeEventArgs> StateChange_Event;
- protected virtual void OnStateChange(StateChangeEventArgs e) {
- EventHandler<StateChangeEventArgs> handler = StateChange_Event;
- if (handler != null) {
- handler(this, e);
- }
- }
- public event EventHandler<StartEventArgs> Start_Event;
- protected virtual void OnStart(StartEventArgs e) {
- EventHandler<StartEventArgs> handler = Start_Event;
- if (handler != null) {
- handler(this, e);
- }
- }
- public event EventHandler<StopEventArgs> Stop_Event;
- protected virtual void OnStop(StopEventArgs e) {
- EventHandler<StopEventArgs> handler = Stop_Event;
- if (handler != null) {
- handler(this, e);
- }
- }
- public ProcessInfo(string processPath) {
- this._process = new Process();
- this.InitProcess(processPath);
- this._process.Exited += _process_Exited;
- this._process.EnableRaisingEvents = true;
- this.initTimer();
- this._state = eState.NotRunning;
- }
- public ProcessInfo(string processPath, bool Autostart) {
- this._process = new Process();
- this.InitProcess(processPath);
- this._process.Exited += _process_Exited;
- this._process.EnableRaisingEvents = true;
- this.initTimer();
- if(Autostart && this._state != eState.Already_Running) {
- this.Start();
- }
- }
- public ProcessInfo(string processPath, EventHandler<StateChangeEventArgs> StateChangeEvent) {
- this._process = new Process();
- this.InitProcess(processPath);
- this._process.Exited += _process_Exited;
- this._process.EnableRaisingEvents = true;
- this.initTimer();
- this.StateChange_Event = StateChangeEvent;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- }
- public ProcessInfo(string processPath, bool Autostart, EventHandler<StateChangeEventArgs> StateChangeEvent) {
- this._process = new Process();
- this.InitProcess(processPath);
- this._process.Exited += _process_Exited;
- this._process.EnableRaisingEvents = true;
- this.initTimer();
- this.StateChange_Event = StateChangeEvent;
- if (Autostart && this._state != eState.Already_Running) {
- this.Start();
- } else {
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- }
- }
- private void InitProcess(string path) {
- this._fi = new System.IO.FileInfo(path);
- Process[] currentRunning = Process.GetProcesses();
- bool bFound = false;
- string xpath = this._fi.FullName;
- string name = this._fi.Name;
- if (name.Contains("."))
- name = name.Substring(0, name.LastIndexOf("."));
- foreach (Process p in currentRunning) {
- if(p.ProcessName == name && p.MainModule.FileName == xpath) {
- bFound = true;
- this._process = Process.GetProcessById(p.Id);
- this._state = eState.Already_Running;
- break;
- }
- }
- if (!bFound) {
- this._process.StartInfo.FileName = this._fi.FullName;
- this._process.StartInfo.UseShellExecute = true;
- }
- }
- private void initTimer() {
- this._forceExitTimer.Elapsed += Timer_ForceExit;
- this._restartTimer.Elapsed += Timer_Restart;
- this._timer.Elapsed += Timer_CheckProcess;
- }
- private void Timer_CheckProcess(object sender, ElapsedEventArgs e) {
- if(this._process.HasExited && this._state == eState.Running) {
- this._doRestart();
- }
- }
- public void Start() {
- if (this._process != null && this._fi.Exists) {
- this._state = eState.Running;
- OnStart(new StartEventArgs());
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- //BaseCommand.WriteLine("Starte Service: " + this._fi.Name, ConsoleColor.Green);
- this._process.Start();
- } else {
- this._state = eState.NotRunning;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- }
- }
- public void Restart() {
- if (this._process != null && this._fi.Exists) {
- this._state = eState.Restart;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- //BaseCommand.WriteLine("Retarte Service: " + this._fi.Name, ConsoleColor.Yellow);
- this._doRestart();
- }
- }
- public string ReadOutput() {
- if (this._process != null) {
- if (this._process.StandardOutput == null)
- return "";
- return this._process.StandardOutput.ReadToEnd();
- }
- return "";
- }
- private void _doRestart() {
- if (this._process != null && this._fi.Exists) {
- if (this._state == eState.Running) {
- // Restart application
- this._state = eState.Restart;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- this._restartTimer.Start();
- } else if (this._state == eState.Restart) {
- this._restartTimer.Start();
- }
- }
- }
- public void Stop() {
- if (this._process != null && this._fi.Exists) {
- this._state = eState.Stopping;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- if (!string.IsNullOrEmpty(this._uri)) {
- client.Get_String(this._uri + "/qqq.cgi");
- this._forceExitTimer.Start();
- } else {
- this._process.Kill();
- this._process.Close();
- this._state = eState.Stopped;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- }
- //BaseCommand.WriteLine("Stop Service: " + this._fi.Name, ConsoleColor.Red);
- }
- }
- private void Timer_ForceExit(object sender, ElapsedEventArgs e) {
- try {
- this._process.Kill();
- } catch(Exception ex) {
- }
- this._process.Close();
- this._forceExitTimer.Stop();
- this._state = eState.Stopped;
- OnStateChange(new StateChangeEventArgs() { State = this._state });
- }
- private void _process_Exited(object sender, EventArgs e) {
- //BaseCommand.WriteLine("Service was exit: " + this._fi.Name, ConsoleColor.Red);
- this._doRestart();
- }
- private void Timer_Restart(object sender, ElapsedEventArgs e) {
- this._restartTimer.Stop();
- //BaseCommand.WriteLine("About to restart: " + this._fi.Name, ConsoleColor.Green);
- this.Start();
- }
- public eState State {
- get { return this._state; }
- }
- public TimeSpan TotalProcessorTime {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.TotalProcessorTime;
- return new TimeSpan(0);
- }
- }
- public long VirtualMemorySize64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.VirtualMemorySize64;
- return 0;
- }
- }
- public TimeSpan UserProcessorTime {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.UserProcessorTime;
- return new TimeSpan(0);
- }
- }
- public DateTime StartTime {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.StartTime;
- return new DateTime();
- }
- }
- public string ProcessName {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.ProcessName;
- return "";
- }
- }
- public long PrivateMemorySize64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.PrivateMemorySize64;
- return 0;
- }
- }
- public long PeakVirtualMemorySize64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.PeakVirtualMemorySize64;
- return 0;
- }
- }
- public long PeakWorkingSet64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.PeakWorkingSet64;
- return 0;
- }
- }
- public long PeakPagedMemorySize64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.PeakPagedMemorySize64;
- return 0;
- }
- }
- public long PagedSystemMemorySize64 {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.PagedSystemMemorySize64;
- return 0;
- }
- }
- public long PagedMemorySize64 {
- get {
- if(this._process != null && this._state == eState.Running)
- return this._process.PagedMemorySize64;
- return 0;
- }
- }
- public int ExitCode {
- get {
- if (this._process != null && this._state == eState.Running)
- return this._process.ExitCode;
- return -1;
- }
- }
- }
- }
|