如何分配单独的线程为每个文件系统观察? [英] How to assign separate thread for each File System Watcher?

查看:124
本文介绍了如何分配单独的线程为每个文件系统观察?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个<一个href="http://stackoverflow.com/questions/1575155/what-is-a-database-file-system/1659772#1659772">database 的。它的文件系统包括多目录观察者这是一个窗口服务和它使用的文件系统观察类。

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.

我想运行在不同的thread.Thread每个观察类进行扩展,.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?

修改 -

以下是我的基地观察者类。

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

以下是从WatcherBase类我的目录观察家类扩展

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

我停留在这个point.Please帮助我。

I am stuck at this point.Please help me.

推荐答案

在一个单独的线程的 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

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

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.

这篇关于如何分配单独的线程为每个文件系统观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆