File System Watcher within threads

G

Guest

Hello -
My question involves using a FileSystemWatcher class within threads in a
process. I am starting multiple threads, and launching a method I called
"Start" for each; within each thread, I want to monitor a directory for new
files, using FileSystemWatcher. The OnCreate handler for the FSW will then
process the file. The code works, but the OnCreate handlers are NOT
independent of each other - if the processing of one file takes 30 seconds,
the processing of another file in a different directory doesn't start until
the first one is done. It seems that the OnCreate handler code is not
running on the same thread that I "Start"ed, and where the FSW class was
instantiated. Instead, it seems that all of the OnCreate handler code
instances are running on the same thread (hence the blocking problem). Is
there a way around this? I really need each thread and its OnCreate handler
code to be independent of each other.

Thanks in advance.
 
M

Mr. Arnold

ReynoldsUser said:
Hello -
My question involves using a FileSystemWatcher class within threads in a
process. I am starting multiple threads, and launching a method I called
"Start" for each; within each thread, I want to monitor a directory for
new
files, using FileSystemWatcher. The OnCreate handler for the FSW will
then
process the file. The code works, but the OnCreate handlers are NOT
independent of each other - if the processing of one file takes 30
seconds,
the processing of another file in a different directory doesn't start
until
the first one is done. It seems that the OnCreate handler code is not
running on the same thread that I "Start"ed, and where the FSW class was
instantiated. Instead, it seems that all of the OnCreate handler code
instances are running on the same thread (hence the blocking problem). Is
there a way around this? I really need each thread and its OnCreate
handler
code to be independent of each other.

Thanks in advance.

Is the same FSW being used by all threads?
 
G

Guest

No. Each thread creates its own FSW object, and begins monitoring a
different directory from all the other threads. I do use the same OnCreate
handler method for all FSW objects that get created.
 
M

Mr. Arnold

ReynoldsUser said:
No. Each thread creates its own FSW object, and begins monitoring a
different directory from all the other threads. I do use the same
OnCreate
handler method for all FSW objects that get created.

It seems to me you need to use a OnCreated EventHanderler for each FSW.
There is no reason you cannot do that I would think.

You might try it for 2 or 3 threads pointing to different OnCreated Named
delegates to see what happens to free the bottleneck of only using one for
all threads.
 
G

Guest

Sorry, I'm not exactly sure I understand your suggestion. I have the main
program that instantiates multiple objects of the same class. The class
definition is where I launch the thread method, and the thread method in turn
instantiates the FSW and sets up the OnCreated handler. The OnCreated
handler method is contained within the class code, so I get the same code
with each class instantiation. How would I define a *different* handler for
each class instance?

Do you mean writing several (identical) methods within the class, and using
a different handler method for each class I instantiate, for testing purposes?
 
M

Mr. Arnold

ReynoldsUser said:
Sorry, I'm not exactly sure I understand your suggestion. I have the
main
program that instantiates multiple objects of the same class. The class
definition is where I launch the thread method, and the thread method in
turn
instantiates the FSW and sets up the OnCreated handler. The OnCreated
handler method is contained within the class code, so I get the same code
with each class instantiation. How would I define a *different* handler
for
each class instance?

Do you mean writing several (identical) methods within the class, and
using
a different handler method for each class I instantiate, for testing
purposes?

Yes, that's what I mean protected void OnCreated1(args) 2, and 3 and point
FileSystemEventHandlers to the OnCreated(s).
 
G

Guest

Understood - but unfortunately that didn't change the behavior. The
executable starts up and uses thread ID 4, and starts up 2 worker threads,
which get IDs of 5 and 7 respectively. However, the events handled from
either thread get executed on thread ID 6, even though I've got separate
handler methods for each.
 

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