绑定事件在另一个类中

时间:2011-07-08 20:25:48

标签: c# events filesystems

这是我的第一篇C#帖子。

我对事件绑定有疑问。

我有一个FileWatcher我想绑定到一个名为FileWatcherEvents的单独类中定义的函数。

我不希望在Program类中声明事件,如何做到这一点?

如您所见,我尝试绑定CreatedDeleted的事件。

问题是当我在文件夹中删除或创建文件时,不会调用这些事件。但是当我在Program类中声明事件处理程序时,它确实有效。

感谢任何帮助或见解。

程序


using System.IO;

class Program : ServiceBase
{
    private FileSystemWatcher _watcher;

    public Program()
    {
        FileWatcherEvents fwe = new FileWatcherEvents();
        this._watcher = new FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize)(this._watcher)).BeginInit();
        // 
        // _watcher
        // 
        this._watcher.EnableRaisingEvents = true;
        this._watcher.Filter = "*.txt";
        this._watcher.NotifyFilter =
        ((NotifyFilters)(((((NotifyFilters.FileName 
            | NotifyFilters.DirectoryName)
            | NotifyFilters.LastWrite)
            | NotifyFilters.LastAccess)
            | NotifyFilters.CreationTime)));
        this._watcher.Path = "T:\\out\\";
        this._watcher.Deleted += new FileSystemEventHandler(fwe.ShipmentFileCreated);
        this._watcher.Created += new FileSystemEventHandler(fwe.FileDeleted);
        ((System.ComponentModel.ISupportInitialize)(this._watcher)).EndInit();
    }

    static void Main(string[] args)
    {
        Program prg = new Program();
        Console.Write(FileManager.getNewestFile("T:\\out\\"));
        while (Console.Read() != 'q') ;
    }
}

FileWatcherEvents


class FileWatcherEvents
{
    public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
    }

    public void FileDeleted(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
    }
}

2 个答案:

答案 0 :(得分:1)

我相信您需要在更大的范围内声明fwe,例如在Program级别而不是在Program构造函数内部。否则,对象将消失,并且可能还会消除导致它的所有事件(当实例消失时,对于处理事件上的事件的函数会发生什么事情,从未完全清楚,但事件仍然可能发生,但它是很可能他们将不再运行。

修改 我让你的代码进行一些微小的调整。主要是我必须将EnableRaisingEvents移动到块的末尾,因为如果在设置路径之前执行此操作,则.NET会引发异常。你怎么没有看到那个例外?

class Program
{
    private FileSystemWatcher _watcher;

    public Program()
    {
        FileWatcherEvents fwe = new FileWatcherEvents();

        this._watcher = new System.IO.FileSystemWatcher();
        this._watcher.Filter = "*.txt";
        this._watcher.NotifyFilter = ((System.IO.NotifyFilters)(((((
            System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName) 
            | System.IO.NotifyFilters.LastWrite) | System.IO.NotifyFilters.LastAccess) 
            | System.IO.NotifyFilters.CreationTime)));
        this._watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
        this._watcher.Deleted += new System.IO.FileSystemEventHandler(fwe.ShipmentFileCreated); 
        this._watcher.Created += new System.IO.FileSystemEventHandler(fwe.FileDeleted);
        this._watcher.EnableRaisingEvents = true;
        Console.ReadLine();
    }

    public static void Main()
    {
        Program prg = new Program();
        using (System.IO.StreamWriter w = new System.IO.StreamWriter(
            System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TestFile.txt"), false))
        {
            w.WriteLine(DateTime.Now.ToLongTimeString());
        }
        Console.ReadLine();
    }
}

class FileWatcherEvents
{
    public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
    }

    public void FileDeleted(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
    }
}

答案 1 :(得分:0)

事实证明代码工作正常,事件被触发,但函数不是因为私有*.txt对象中的FileSystemWatcher过滤器。