470,630 Members | 1,671 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,630 developers. It's quick & easy.

Multiple FileSystemWatchers, monitoring the same path

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
Nov 22 '05 #1
1 1629
I solved the problem by creating my own FileSystemWatcher (non-derived) and
thus, pulled the contents of a folder every n second.

"Fredrik Johansson" wrote:
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

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ken Madden | last post: by
reply views Thread by Jeff Reed | last post: by
2 posts views Thread by daniel | last post: by
35 posts views Thread by keerthyragavendran | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.