473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1780
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Madden | last post by:
I want to create multiple fileSystemWatchers in a Windows Service to constantly watch multiple folders for file creations and then execute certain code based on these actions. The problem is that...
5
by: zfeld | last post by:
I am serializing an object to XML and writing it to disk. whenever a change to my object occurs I call the save function to re-write to disk (see code below). I am monitoring the directory where...
0
by: Jeff Reed | last post by:
I am experiencing the the problem outlined the below. Unfortunately, I am using WinXP and I not sure if I can apply the solution due to lack of security control Any feed back would be apreciated ...
2
by: Greg Allen | last post by:
I know this has been discussed before, and have found some documentation about it on the web. But nothing has fixed my problem. I am running the 1.1 .NET framework, SP1. I have a web...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
1
by: Mika M | last post by:
Hi! I've made FileSystemWatcher application to copy files from one UNC-path (\\server\directory) into another when files appears into source path. I made it using link...
2
by: daniel | last post by:
Hi, I am developing an application that requires me to set up file system watchers on directories on a network drive, as well as set up CacheDependencies in the same location for my...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
1
by: D2 | last post by:
Hi All, I'm just wondering whether a FileSystemWatcher object can be used to monitor multiple directores or we have to create one FileSystemWatcher object for each folder we need to monitor? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.