Reputation: 6432
I am developing a database file system.It includes a multi-directory watcher which is a windows service and which uses the file system watcher class from .net.
I want to run each watcher class on separate thread.Thread can not be extended in .net because as it is "Sealed". What I want is, run all the methods of my watcher class in the related thread. How can I achieve this?
EDIT -
Following is my base watcher class.
public abstract class WatcherBase
{
private IWatchObject _watchObject;
public WatcherBase() { }
public WatcherBase(IWatchObject watchObject, bool canPauseAndContinue)
{
_watchObject = watchObject;
CanPauseAndContinue = canPauseAndContinue;
}
public bool CanPauseAndContinue { get; set; }
public IWatchObject ObjectToWatch
{
get
{
return _watchObject;
}
}
public abstract void Start();
public abstract void Pause();
public abstract void Continue();
public abstract void Stop();
}
Following is my directory watcher class extended from the WatcherBase class
namespace RankFs.WatcherService
{
public class DirectoryWatcher : WatcherBase
{
private WatchDirectory _directoryToWatch;
private FileSystemWatcher _watcher;
public DirectoryWatcher(WatchDirectory directory, bool CanPauseAndContinue)
:base(directory ,CanPauseAndContinue)
{
_directoryToWatch = directory;
_watcher = new FileSystemWatcher(_directoryToWatch.Path);
_watcher.IncludeSubdirectories = _directoryToWatch.WatchSubDirectories;
_watcher.Created +=new FileSystemEventHandler(Watcher_Created);
//_watcher.Changed +=new FileSystemEventHandler(Watcher_Changed);
_watcher.Deleted +=new FileSystemEventHandler(Watcher_Deleted);
_watcher.Renamed +=new RenamedEventHandler(Watcher_Renamed);
}
public WatchDirectory DirectoryToWatch
{
get
{
return _directoryToWatch;
}
}
public override void Start()
{
_watcher.EnableRaisingEvents = true;
}
public override void Pause()
{
_watcher.EnableRaisingEvents = false;
}
public override void Continue()
{
_watcher.EnableRaisingEvents = true;
}
public override void Stop()
{
_watcher.EnableRaisingEvents = false;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
// adds a new file entry to database
}
private void Watcher_Deleted(object sender, FileSystemEventArgs e)
{
//updates the database(deleted file)
}
private void Watcher_Renamed(object sender, RenamedEventArgs e)
{
//updates the database(renamed file)
}
} }
I am stuck at this point.Please help me.
Upvotes: 3
Views: 14383
Reputation: 57698
The FileSystemWatcher
will trigger the events in a separate thread. The logic inside the event handlers will need to take that fact in consideration and perform any synchronization needed.
If you run the following example you will see that the Changed
event handler will run in a different thread.
public static void Main(string[] args)
{
Directory.CreateDirectory("dir1");
Directory.CreateDirectory("dir2");
Directory.CreateDirectory("dir3");
Console.WriteLine("Main Thread Id: {0}",
Thread.CurrentThread.ManagedThreadId);
const int watcherCount = 3;
string[] dirs = new string[] { "dir1", "dir2", "dir3" };
for (int i = 0; i < watcherCount; i++)
{
var watcher = new FileSystemWatcher();
watcher.Path = dirs[i];
watcher.Changed += (sender, e) =>
{
Console.WriteLine("File: {0} | Thread: {1}", e.Name,
Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000); // Simulate long operation
};
watcher.EnableRaisingEvents = true;
}
File.WriteAllText(@"dir1\test1", "hello");
File.WriteAllText(@"dir2\test2", "hello");
File.WriteAllText(@"dir3\test3", "hello");
Thread.Sleep(10000);
}
When running this sample I obtained the following output:
// Main Thread Id: 1
// File: test1 | Thread: 3
// File: test2 | Thread: 4
// File: test3 | Thread: 5
// File: test1 | Thread: 3
// File: test2 | Thread: 4
// File: test3 | Thread: 5
UPDATE:
Since you are using the events approach in the FileSystemWatcher
you are already using a multithreaded approach because the event handlers for each FileSystemWatcher
instance will be triggered in a separate thread in an asynchronous way.
Upvotes: 13
Reputation: 49984
I would write a new class that wraps or adapts the FileSystemWatcher, and then launch my new class on a new thread. The adapter class will handle the notifications from the FSW class, and you can then work out the best way to broadcast or relay those notifications to some sort of marshalling function.
There are a few different ways to launch new threads and billions of tutorials on how to do it, so i'm not going to cover that here in this answer.
Upvotes: 0