如何为每个文件系统观察程序分配单独的线程?

发布于 2024-08-23 04:02:54 字数 2823 浏览 4 评论 0原文

我正在开发一个数据库文件系统。它包括多个-directory watcher,它是一个 Windows 服务,使用 文件系统来自 .net 的 watcher 类。

我想在单独的线程上运行每个观察程序类。线程无法在.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)
    }
} }

我陷入了这一点。请帮助我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-08-30 04:02:54

FileSystemWatcher 将在单独的线程中触发事件。事件处理程序内部的逻辑需要考虑这一事实并执行所需的任何同步。

如果运行以下示例,您将看到 Changed 事件处理程序将在不同的线程中运行。

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);
}

运行此示例时,我获得了以下输出:

// 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

更新:
由于您在 FileSystemWatcher 中使用事件方法,因此您已经在使用多线程方法,因为每个 FileSystemWatcher 实例的事件处理程序将以异步方式在单独的线程中触发。

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.

豆芽 2024-08-30 04:02:54

我会编写一个新类来包装或适应 FileSystemWatcher,然后在一个新线程。适配器类将处理来自 FSW 类的通知,然后您可以找出将这些通知广播或中继到某种编组函数的最佳方法。

有几种不同的方法可以启动新线程,并且有数十亿个关于如何执行此操作的教程,因此我不会在本答案中介绍这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文