~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/ClientUtilities/util/AssemblyWatcher.cs

  • Committer: Charlie Poole
  • Date: 2010-12-11 17:44:33 UTC
  • Revision ID: charlie@nunit.org-20101211174433-06taqojyso8khx62
Eliminate multiple reloading of tests when assembly changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
using System;
8
8
using System.IO;
9
9
using System.Timers;
 
10
using NUnit.Core;
10
11
 
11
12
namespace NUnit.Util
12
13
{
20
21
        /// </summary>
21
22
        public class AssemblyWatcher : IAssemblyWatcher
22
23
        {
23
 
                private FileSystemWatcher[] fileWatchers;
 
24
        static Logger log = InternalTrace.GetLogger(typeof(AssemblyWatcher));
 
25
 
 
26
        private FileSystemWatcher[] fileWatchers;
24
27
                private FileInfo[] files;
25
28
                private bool isWatching;
26
29
 
43
46
        public void Setup(int delay, System.Collections.IList assemblies)
44
47
#endif
45
48
                {
 
49
            log.Info("Setting up watcher");
 
50
 
46
51
                        files = new FileInfo[assemblies.Count];
47
52
                        fileWatchers = new FileSystemWatcher[assemblies.Count];
48
53
 
49
54
                        for (int i = 0; i < assemblies.Count; i++)
50
55
                        {
 
56
                log.Debug("Setting up FileSystemWatcher for {0}", assemblies[i]);
 
57
                
51
58
                                files[i] = new FileInfo((string)assemblies[i]);
52
59
 
53
60
                                fileWatchers[i] = new FileSystemWatcher();
76
83
 
77
84
                private void EnableWatchers( bool enable )
78
85
                {
79
 
                        if (ReferenceEquals(fileWatchers, null))
80
 
                                return;
81
 
 
82
 
                        foreach( FileSystemWatcher watcher in fileWatchers )
83
 
                                watcher.EnableRaisingEvents = enable;
 
86
            if (fileWatchers != null)
 
87
                        foreach( FileSystemWatcher watcher in fileWatchers )
 
88
                                watcher.EnableRaisingEvents = enable;
84
89
 
85
90
                        isWatching = enable;
86
91
                }
87
92
 
88
93
                public void FreeResources()
89
94
                {
90
 
                        if (isWatching)
91
 
                        {
92
 
                                EnableWatchers(false);
93
 
                        }
94
 
 
95
 
                        if (!ReferenceEquals(fileWatchers, null))
 
95
            log.Info("FreeResources");
 
96
 
 
97
            Stop();
 
98
 
 
99
                        if (fileWatchers != null)
96
100
                        {
97
101
                                foreach (FileSystemWatcher watcher in fileWatchers)
98
102
                                {
99
 
                                        if (ReferenceEquals(watcher, null))
100
 
                                                continue;
101
 
 
102
 
                                        watcher.Changed -= new FileSystemEventHandler(OnChanged);
103
 
                                        watcher.Dispose();
 
103
                    if (watcher != null)
 
104
                    {
 
105
                        watcher.Changed -= new FileSystemEventHandler(OnChanged);
 
106
                        watcher.Dispose();
 
107
                    }
104
108
                                }
105
109
                        }
106
110
 
107
 
                        if (!ReferenceEquals(timer, null))
 
111
                        if (timer != null)
108
112
                        {
109
113
                                timer.Stop();
110
114
                                timer.Close();
120
124
                {
121
125
                        lock(this)
122
126
                        {
 
127
                log.Info("Timer expired");
123
128
                                PublishEvent();
124
129
                                timer.Enabled=false;
125
130
                        }
127
132
                
128
133
                protected void OnChanged(object source, FileSystemEventArgs e)
129
134
                {
 
135
            log.Info("File {0} changed", e.Name);
 
136
 
130
137
                        changedAssemblyPath = e.FullPath;
131
138
                        if ( timer != null )
132
139
                        {
134
141
                                {
135
142
                                        if(!timer.Enabled)
136
143
                                                timer.Enabled=true;
 
144
                    log.Info("Setting timer");
137
145
                                        timer.Start();
138
146
                                }
139
147
                        }
145
153
        
146
154
                protected void PublishEvent()
147
155
                {
148
 
                        if ( AssemblyChanged != null )
149
 
                                AssemblyChanged( changedAssemblyPath );
 
156
            if (AssemblyChanged != null)
 
157
            {
 
158
                log.Debug("Publishing Event to {0} listeners", AssemblyChanged.GetInvocationList().Length);
 
159
                AssemblyChanged(changedAssemblyPath);
 
160
            }
150
161
                }
151
162
        }
152
163
}
 
 
b'\\ No newline at end of file'