| 1 |
using System; |
| 2 |
using System.Threading; |
| 3 |
using dbg = SeedCreate.CommonLibrary.DebugHelper; |
| 4 |
using diag = System.Diagnostics; |
| 5 |
|
| 6 |
namespace Toast.ProcessControl |
| 7 |
{ |
| 8 |
public class ProcessWatcher:IDisposable |
| 9 |
{ |
| 10 |
private static readonly TimeSpan DefaultDelay = new TimeSpan(0, 0, 1); |
| 11 |
|
| 12 |
private readonly object syncRoot_ = new object(); |
| 13 |
private Timer timer_; |
| 14 |
private bool isRun_; |
| 15 |
|
| 16 |
private bool isTargetProcessRun_; |
| 17 |
private int CheckCount_; |
| 18 |
|
| 19 |
public event EventHandler DetectProcessStart; |
| 20 |
public event EventHandler DetectProcessTerminate; |
| 21 |
|
| 22 |
public ProcessWatcher() |
| 23 |
{ |
| 24 |
timer_ = new Timer(new TimerCallback(TimerMain)); |
| 25 |
isRun_ = false; |
| 26 |
TargetProcessName = null; |
| 27 |
isTargetProcessRun_ = false; |
| 28 |
CheckCountThreshold = 0; |
| 29 |
CheckCount_ = 0; |
| 30 |
} |
| 31 |
|
| 32 |
public string TargetProcessName |
| 33 |
{ |
| 34 |
get; |
| 35 |
private set; |
| 36 |
} |
| 37 |
|
| 38 |
public int CheckCountThreshold |
| 39 |
{ |
| 40 |
get; |
| 41 |
private set; |
| 42 |
} |
| 43 |
|
| 44 |
public TimeSpan Interval |
| 45 |
{ |
| 46 |
get; |
| 47 |
private set; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public bool Start(int checkCount,TimeSpan interval, string processName) |
| 54 |
{ |
| 55 |
if (interval.Ticks <= 0) |
| 56 |
{ |
| 57 |
throw new ArgumentOutOfRangeException("interval"); |
| 58 |
} |
| 59 |
|
| 60 |
if(processName==null) |
| 61 |
{ |
| 62 |
throw new ArgumentNullException("processName"); |
| 63 |
} |
| 64 |
|
| 65 |
if (checkCount < 0) |
| 66 |
{ |
| 67 |
throw new ArgumentOutOfRangeException("checkCount"); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
lock (syncRoot_) |
| 72 |
{ |
| 73 |
if (isRun_) |
| 74 |
{ |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
TargetProcessName = processName; |
| 79 |
|
| 80 |
isRun_ = true; |
| 81 |
isTargetProcessRun_ = false; |
| 82 |
CheckCountThreshold = checkCount; |
| 83 |
Interval = interval; |
| 84 |
|
| 85 |
timer_.Change(DefaultDelay, interval); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
private void TimerMain(object dummy) |
| 94 |
{ |
| 95 |
lock (syncRoot_) |
| 96 |
{ |
| 97 |
timer_.Change(Timeout.Infinite, Timeout.Infinite); |
| 98 |
|
| 99 |
diag::Process[] Ret= diag::Process.GetProcessesByName(TargetProcessName); |
| 100 |
|
| 101 |
if (Ret.Length != 0 && !isTargetProcessRun_) |
| 102 |
{ |
| 103 |
OnDetectProcessStart(this, EventArgs.Empty); |
| 104 |
isTargetProcessRun_ = true; |
| 105 |
} |
| 106 |
else if (Ret.Length == 0 && isTargetProcessRun_) |
| 107 |
{ |
| 108 |
dbg::TinyDebugWriter.WriteLine("CheckCouht:{0}", CheckCount_); |
| 109 |
if (++CheckCount_ >= CheckCountThreshold) |
| 110 |
{ |
| 111 |
OnDetectProcessTerminated(this, EventArgs.Empty); |
| 112 |
isTargetProcessRun_ = false; |
| 113 |
CheckCount_ = 0; |
| 114 |
} |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
CheckCount_ = 0; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
timer_.Change(Interval, Interval); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private void OnDetectProcessStart(object sender, EventArgs e) |
| 127 |
{ |
| 128 |
dbg::TinyDebugWriter.WriteLine("Detect process start"); |
| 129 |
|
| 130 |
if (DetectProcessStart != null) |
| 131 |
{ |
| 132 |
DetectProcessStart(sender, e); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
private void OnDetectProcessTerminated(object sender, EventArgs e) |
| 137 |
{ |
| 138 |
dbg::TinyDebugWriter.WriteLine("Detect process terminate"); |
| 139 |
|
| 140 |
if (DetectProcessTerminate != null) |
| 141 |
{ |
| 142 |
DetectProcessTerminate(sender, e); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#region IDisposable メンバ |
| 147 |
|
| 148 |
public void Dispose() |
| 149 |
{ |
| 150 |
lock (syncRoot_) |
| 151 |
{ |
| 152 |
timer_.Change(Timeout.Infinite, Timeout.Infinite); |
| 153 |
DetectProcessTerminate = null; |
| 154 |
DetectProcessStart = null; |
| 155 |
timer_.Dispose(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
#endregion |
| 160 |
} |
| 161 |
} |