Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 47020df

Browse files
committed
Merge pull request #2450 from stephentoub/fix_fsw_ode
Fix ObjectDisposedException in FileSystemWatcher tests
2 parents bbbf587 + 43bfad6 commit 47020df

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Deleted.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ public static void FileSystemWatcher_Deleted_NestedDirectories()
8686
{
8787
using (var dir = Utility.CreateTestDirectory())
8888
using (var watcher = new FileSystemWatcher())
89-
using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
90-
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
9189
{
90+
AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
91+
AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted);
92+
9293
watcher.Path = Path.GetFullPath(dir.Path);
9394
watcher.Filter = "*";
9495
watcher.IncludeSubdirectories = true;
@@ -125,9 +126,10 @@ public static void FileSystemWatcher_Deleted_FileDeletedInNestedDirectory()
125126
{
126127
using (var dir = Utility.CreateTestDirectory())
127128
using (var watcher = new FileSystemWatcher())
128-
using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
129-
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
130129
{
130+
AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
131+
AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted);
132+
131133
watcher.Path = Path.GetFullPath(dir.Path);
132134
watcher.Filter = "*";
133135
watcher.IncludeSubdirectories = true;

src/System.IO.FileSystem.Watcher/tests/Utility/Utility.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,16 @@ public static void ExpectNoEvent(WaitHandle eventOccured, string eventName, int
102102
}
103103

104104
public static void TestNestedDirectoriesHelper(
105-
WatcherChangeTypes change,
106-
Action<AutoResetEvent, TemporaryTestDirectory> action,
107-
NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName)
105+
WatcherChangeTypes change,
106+
Action<AutoResetEvent, TemporaryTestDirectory> action,
107+
NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName)
108108
{
109109
using (var dir = Utility.CreateTestDirectory())
110110
using (var watcher = new FileSystemWatcher())
111-
using (AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
112-
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, change))
113111
{
112+
AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
113+
AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, change);
114+
114115
watcher.Path = Path.GetFullPath(dir.Path);
115116
watcher.Filter = "*";
116117
watcher.NotifyFilter = changeFilers;

0 commit comments

Comments
 (0)