Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System / System.IO / DefaultWatcher.cs
index 81ae631e6e4177effd482be99ed179c4e355862b..8988a6b97030e2180008f7c7551ac043e366a0a3 100644 (file)
@@ -63,18 +63,17 @@ namespace System.IO {
                {
                }
                
+               // Locked by caller
                public static bool GetInstance (out IFileWatcher watcher)
                {
-                       lock (typeof (DefaultWatcher)) {
-                               if (instance != null) {
-                                       watcher = instance;
-                                       return true;
-                               }
-
-                               instance = new DefaultWatcher ();
+                       if (instance != null) {
                                watcher = instance;
                                return true;
                        }
+
+                       instance = new DefaultWatcher ();
+                       watcher = instance;
+                       return true;
                }
                
                public void StartDispatching (FileSystemWatcher fsw)
@@ -117,6 +116,10 @@ namespace System.IO {
                public void StopDispatching (FileSystemWatcher fsw)
                {
                        DefaultWatcherData data;
+                       lock (this) {
+                               if (watches == null) return;
+                       }
+                       
                        lock (watches) {
                                data = (DefaultWatcherData) watches [fsw];
                                if (data != null) {
@@ -145,7 +148,7 @@ namespace System.IO {
                                        my_watches = (Hashtable) watches.Clone ();
                                }
                                
-                               if (watches.Count != 0) {
+                               if (my_watches.Count != 0) {
                                        zeroes = 0;
                                        foreach (DefaultWatcherData data in my_watches.Values) {
                                                bool remove = UpdateDataAndDispatch (data, true);