Does FileSystemWatcher create its own thread?

11,352

You don't have to create a thread. The events will be called on a separate thread automatically.

Share:
11,352

Related videos on Youtube

syncis
Author by

syncis

Updated on June 11, 2022

Comments

  • syncis
    syncis almost 2 years

    I want this work to be done in a different thread but do i have to create a thread or does it do all the work on different threads?

    Like:

    Thread fileThread = new Thread(() =>
    {
        FileWatcher = new FileSystemWatcher();
    
        FileWatcher.Created += OnFileEvent;
        FileWatcher.Deleted += OnFileEvent;
        FileWatcher.Renamed += OnRenameEvent;
        FileWatcher.EnableRaisingEvents = true;
    });
    
    fileThread.Start();
    
  • syncis
    syncis almost 12 years
    ok perfekt, i got another question: Once i stop the watcher, i set enableraisingeevents = false, but when i want to start the watcher again , i dont know if i should just enableraisingevents=true or if i should create a new instance of the filesystemwatcher like the above code, what do you think? Thank you
  • Guffa
    Guffa almost 12 years
    @syncis: If you keep the instance, you can just turn it on again.
  • syncis
    syncis almost 12 years
    Ok my concern was that i dont want it to do any work at some point and if its enableraisingevents = false , i thought it would do all the work but just not raise the events.
  • Guffa
    Guffa almost 12 years
    @syncis: If you are concerned about that, then you should create a new object instead. However, just turning off enableraisingevents will not stop it from listening for changes, you should call Dispose when you don't use it any more.
  • Daniel Möller
    Daniel Möller over 10 years
    How would the debugger handle that?
  • Ahmad Ibrahim
    Ahmad Ibrahim almost 7 years
    @Guffa Why do you think that turning off EnableRaisingEvents will not stop it from listening? What I can see in the source code is that it will.