Multiple FileSystemWatchers, monitoring the same path

G

Guest

Hello,

I have built a .NET remoting solution (SingleCall) that writes a file to
disk, then waits for a second file in a specified folder (using the
FileSystemWatcher.WaitForChanged method). When the component is called by two
peers at the same time, two FileSystemWatchers are created (one for each
peer). They are both monitoring the same path (but for different files -
using different filters). The following code illustrates the scenario:

using System;
using System.IO;
using System.Threading;

class clsMain {
static void Main() {
Thread thread1 = new Thread(new ThreadStart(t1));
Thread thread2 = new Thread(new ThreadStart(t2));
thread1.Start();
thread2.Start();

Console.ReadLine();
}

static void t1() {
FileSystemWatcher fs1 = new FileSystemWatcher(@"c:\files", "file1");
if (!fs1.WaitForChanged(System.IO.WatcherChangeTypes.All, 10000).TimedOut) {
System.Console.WriteLine("f1 found!");
} else {
System.Console.WriteLine("f1 not found!");
}
}

static void t2() {
System.IO.FileSystemWatcher fs2 = new
System.IO.FileSystemWatcher(@"c:\files", "file2");
if (!fs2.WaitForChanged(System.IO.WatcherChangeTypes.All, 10000).TimedOut) {
System.Console.WriteLine("f2 found!");
} else {
System.Console.WriteLine("f2 not found!");
}
}
}

When copying file1 and file2 to the path c:\files (which are monitored by
the two FileSystemWatchers) only one of them receives a signal, the other one
simply timesout.

Please advice,
Best Regards,
Fredrik Johansson
 
G

Guest

I solved the problem by creating my own FileSystemWatcher (non-derived) and
thus, pulled the contents of a folder every n second.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top