我正在开发一个数据库文件系统。它包括一个多目录观察器,它是一个 Windows 服务,它使用来自 .net的文件系统观察器类。
我想在单独的线程上运行每个观察程序类。线程不能在 .net 中扩展,因为它是“密封的”。我想要的是,在相关线程中运行我的观察者类的所有方法。我怎样才能做到这一点?
编辑-
以下是我的基本观察者类。
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();
}
以下是我从 WatcherBase 类扩展的目录观察器类
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)
}
} }
我被困在这一点上。请帮助我。