如何正确提升自定义事件?

时间:2012-04-10 23:41:47

标签: c# events

如果找到/创建了特定目录中的新/现有文件,我需要一个自定义事件。要检查是否创建了新文件,我使用SystemFileWatcher,它可以正常工作。为了检查程序启动时是否存在某些文件,我写了一些行,这是有效的。

我将此类用于事件args:

public class FileDetectEventArgs : EventArgs
{
    public String Source { get; set; }
    public String Destination { get; set; }
    public String FullName { get; set; }

    public FileDetectEventArgs(String source, String destination, String fullName)
    {
        this.Source = source;
        this.Destination = destination;
        this.FullName = fullName;
    }
}

如果SystemFileWatcher引发FileCreated事件,我使用以下代码行:

public void onFileCreated(object sender, FileSystemEventArgs e)
{
    // check if file exist
    if (File.Exists(e.FullPath))
    {
        OnNewFileDetect(new FileDetectEventArgs(source, destination, e.FullPath));  
    }
}

如果文件存在,我尝试以这种方式提出事件:

public void checkExistingFiles(String source, String filter)
    {
        DirectoryInfo di = new DirectoryInfo(source);
        FileInfo[] fileInfos = di.GetFiles();
        String fileFilter = filter.Substring(filter.LastIndexOf('.'));       

        foreach (FileInfo fi in fileInfos)
        {
            if (fi.Extension.Equals(fileFilter))
            {
                OnNewFileDetect(new FileDetectEventArgs(source, destination, fi.FullName));                   
            }
        }
    }

这是OnNewFileDetect-Event:

protected void OnNewFileDetect(FileDetectEventArgs e)
    {
        if (OnNewFileDetectEvent != null)
        {
            OnNewFileDetectEvent(this, e);
        }
    }

问题是,如果onFileCreated-Event引发我的OnNewFileDetect-Event一切正常。但是如果checkExistingFiles找到了一些现有文件并尝试引发OnNewFileDetect-Event,则不会发生任何事情。我发现OnNewFileDetectEvent-Object为null,因此没有任何反应。但是如果onFileCreated-Event被触发,为什么它不为null?

2 个答案:

答案 0 :(得分:2)

  

但是如果onFileCreated-Event被触发,为什么它不为null?

在事件订阅之前,此事件将为null。


另外,我会考虑切换到更好的模式来提升事件,以及在此处使用标准的C#/ .NET命名。这更像是:

// The event...
public EventHandler<FileDetectedEventArgs> NewFileDetected;

// Note the naming
protected void OnNewFileDetected(FileDetectedEventArgs e)
{
    // Note this pattern for thread safety...
    EventHandler<FileDetectedEventArgs> handler = this.NewFileDetected; 
    if (handler != null)
    {
        handler(this, e);
    }
}

答案 1 :(得分:1)

@Reed Copsey回答了这个问题;事件将为null,直到有订阅者为止。

此外,这是一种潜在的竞争条件:

if (OnNewFileDetectEvent != null)
{
    OnNewFileDetectEvent(this, e);
}

请注意,在多线程方案中,如果OnNewFileDetectEventnull语句之后设置为if,但在下一行调用之前,则程序将崩溃。通常你会这样做:

var del = OnNewFileDetectEvent;
if (del != null)
{
    del(this, e);
}

由于您的事件对象是不可变的,del如果在if语句中不为空,则永远不会为空。

另请注意,您的命名是非常规的。事件通常不以On为前缀,调用事件的方法是。