0

When I receive a notification from a FileSystemWatcher, I want to start a separate thread to take care of the further processing.

How do I go about doing Multi-Threading in the FileSystemWatcher Service for Event-Handling ?

Derek
  • 2,185
  • 4
  • 20
  • 24
  • what have you tried ? what doesn't work ? any error messages/exceptions ? please show some source code... – Yahia Aug 29 '11 at 04:41
  • The THing Is I Have No Clue About Multi-Threading & I have not done any coding yet. – Derek Aug 29 '11 at 04:47
  • I would think that you have at least coded the single-thread version ? show that... – Yahia Aug 29 '11 at 04:48
  • Please refer to my the other question for references for single-threaded version. http://stackoverflow.com/questions/7184472/fileinfo-exceptions – Derek Aug 29 '11 at 04:56
  • fromt hat question it seems that the single-threaded version isn't working correctly - I suggest to fix it first and then look for how to make it multi-threaded ! – Yahia Aug 29 '11 at 05:00
  • Could you just advice me on how do I multi-threading for that version first before I correct the neccessary changes on my own ? – Derek Aug 29 '11 at 05:09
  • not really because for multi-threading it could be important what exactly should be done in those threads... if you want I can give some general links about multi-threading with sample code (not related to FileSystemWatcher) - should I do that ? – Yahia Aug 29 '11 at 05:15
  • Look here http://stackoverflow.com/questions/4967095/c-predict-file-system-events-on-folder-delete/4968391#4968391 – adrianm Aug 30 '11 at 06:19

1 Answers1

-1

I don't understand why people were unable to answer this simple question. For any other people wondering about this, here is one way of doing this:

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher fsw = new FileSystemWatcher();
        fsw.Path = @"C:\temp\";
        fsw.Created += new FileSystemEventHandler(onCreatedFile);
        fsw.EnableRaisingEvents = true;

        Console.ReadLine();
    }

    private static void onCreatedFile(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("Thread " + AppDomain.GetCurrentThreadId() + " detected " + e.FullPath);

        Thread t = new Thread(new ParameterizedThreadStart(seperateThread));
        t.Start(e);
    }

    private static void seperateThread(object obj)
    {
        FileSystemEventArgs e = (FileSystemEventArgs)obj;

        Console.WriteLine("Thread " + AppDomain.GetCurrentThreadId() + " detected " + e.FullPath);
    }
}
jzacharuk
  • 2,058
  • 1
  • 16
  • 22
  • According to answers to this question (http://stackoverflow.com/questions/11492720/does-filesystemwatcher-create-its-own-thread) it appears that there is some disagreement about whether FileSystemWatcher raises the event on a new thread or not. I would guess that it depends. – jpierson May 29 '15 at 12:38
  • @jpierson the event handler may be on a new thread, but will still handle each event serially, i.e., after the previous event is completely handled. With the Answer above, each event will be handled in another new thread. – Roland Feb 10 '16 at 13:21