1
// ****************************************************************
2
// This is free software licensed under the NUnit license. You
3
// may obtain a copy of the license as well as information regarding
4
// copyright ownership at http://nunit.org.
5
// ****************************************************************
15
/// AssemblyWatcher keeps track of one or more assemblies to
16
/// see if they have changed. It incorporates a delayed notification
17
/// and uses a standard event to notify any interested parties
18
/// about the change. The path to the assembly is provided as
19
/// an argument to the event handler so that one routine can
20
/// be used to handle events from multiple watchers.
22
public class AssemblyWatcher : IAssemblyWatcher
24
static Logger log = InternalTrace.GetLogger(typeof(AssemblyWatcher));
26
private FileSystemWatcher[] fileWatchers;
27
private FileInfo[] files;
28
private bool isWatching;
30
protected System.Timers.Timer timer;
31
protected string changedAssemblyPath;
33
protected FileInfo GetFileInfo(int index)
38
public void Setup(int delay, string assemblyFileName)
40
Setup(delay, new string[] {assemblyFileName});
43
#if NET_2_0 || NET_4_0
44
public void Setup(int delay, System.Collections.Generic.IList<string> assemblies)
46
public void Setup(int delay, System.Collections.IList assemblies)
49
log.Info("Setting up watcher");
51
files = new FileInfo[assemblies.Count];
52
fileWatchers = new FileSystemWatcher[assemblies.Count];
54
for (int i = 0; i < assemblies.Count; i++)
56
log.Debug("Setting up FileSystemWatcher for {0}", assemblies[i]);
58
files[i] = new FileInfo((string)assemblies[i]);
60
fileWatchers[i] = new FileSystemWatcher();
61
fileWatchers[i].Path = files[i].DirectoryName;
62
fileWatchers[i].Filter = files[i].Name;
63
fileWatchers[i].NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;
64
fileWatchers[i].Changed += new FileSystemEventHandler(OnChanged);
65
fileWatchers[i].EnableRaisingEvents = false;
68
timer = new System.Timers.Timer(delay);
69
timer.AutoReset = false;
70
timer.Enabled = false;
71
timer.Elapsed += new ElapsedEventHandler(OnTimer);
76
EnableWatchers( true );
81
EnableWatchers( false );
84
private void EnableWatchers( bool enable )
86
if (fileWatchers != null)
87
foreach( FileSystemWatcher watcher in fileWatchers )
88
watcher.EnableRaisingEvents = enable;
93
public void FreeResources()
95
log.Info("FreeResources");
99
if (fileWatchers != null)
101
foreach (FileSystemWatcher watcher in fileWatchers)
105
watcher.Changed -= new FileSystemEventHandler(OnChanged);
121
public event AssemblyChangedHandler AssemblyChanged;
123
protected void OnTimer(Object source, ElapsedEventArgs e)
127
log.Info("Timer expired");
133
protected void OnChanged(object source, FileSystemEventArgs e)
135
log.Info("File {0} changed", e.Name);
137
changedAssemblyPath = e.FullPath;
144
log.Info("Setting timer");
154
protected void PublishEvent()
156
if (AssemblyChanged != null)
158
log.Debug("Publishing Event to {0} listeners", AssemblyChanged.GetInvocationList().Length);
159
AssemblyChanged(changedAssemblyPath);
1
// ****************************************************************
2
// This is free software licensed under the NUnit license. You
3
// may obtain a copy of the license as well as information regarding
4
// copyright ownership at http://nunit.org.
5
// ****************************************************************
15
/// AssemblyWatcher keeps track of one or more assemblies to
16
/// see if they have changed. It incorporates a delayed notification
17
/// and uses a standard event to notify any interested parties
18
/// about the change. The path to the assembly is provided as
19
/// an argument to the event handler so that one routine can
20
/// be used to handle events from multiple watchers.
22
public class AssemblyWatcher : IAssemblyWatcher
24
static Logger log = InternalTrace.GetLogger(typeof(AssemblyWatcher));
26
private FileSystemWatcher[] fileWatchers;
27
private FileInfo[] files;
28
private bool isWatching;
30
protected System.Timers.Timer timer;
31
protected string changedAssemblyPath;
33
protected FileInfo GetFileInfo(int index)
38
public void Setup(int delay, string assemblyFileName)
40
Setup(delay, new string[] {assemblyFileName});
43
#if NET_2_0 || NET_4_0
44
public void Setup(int delay, System.Collections.Generic.IList<string> assemblies)
46
public void Setup(int delay, System.Collections.IList assemblies)
49
log.Info("Setting up watcher");
51
files = new FileInfo[assemblies.Count];
52
fileWatchers = new FileSystemWatcher[assemblies.Count];
54
for (int i = 0; i < assemblies.Count; i++)
56
log.Debug("Setting up FileSystemWatcher for {0}", assemblies[i]);
58
files[i] = new FileInfo((string)assemblies[i]);
60
fileWatchers[i] = new FileSystemWatcher();
61
fileWatchers[i].Path = files[i].DirectoryName;
62
fileWatchers[i].Filter = files[i].Name;
63
fileWatchers[i].NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;
64
fileWatchers[i].Changed += new FileSystemEventHandler(OnChanged);
65
fileWatchers[i].EnableRaisingEvents = false;
68
timer = new System.Timers.Timer(delay);
69
timer.AutoReset = false;
70
timer.Enabled = false;
71
timer.Elapsed += new ElapsedEventHandler(OnTimer);
76
EnableWatchers( true );
81
EnableWatchers( false );
84
private void EnableWatchers( bool enable )
86
if (fileWatchers != null)
87
foreach( FileSystemWatcher watcher in fileWatchers )
88
watcher.EnableRaisingEvents = enable;
93
public void FreeResources()
95
log.Info("FreeResources");
99
if (fileWatchers != null)
101
foreach (FileSystemWatcher watcher in fileWatchers)
105
watcher.Changed -= new FileSystemEventHandler(OnChanged);
121
public event AssemblyChangedHandler AssemblyChanged;
123
protected void OnTimer(Object source, ElapsedEventArgs e)
127
log.Info("Timer expired");
133
protected void OnChanged(object source, FileSystemEventArgs e)
135
log.Info("File {0} changed", e.Name);
137
changedAssemblyPath = e.FullPath;
144
log.Info("Setting timer");
154
protected void PublishEvent()
156
if (AssemblyChanged != null)
158
log.Debug("Publishing Event to {0} listeners", AssemblyChanged.GetInvocationList().Length);
159
AssemblyChanged(changedAssemblyPath);
b'\\ No newline at end of file'