简体   繁体   English

通过FileSystemWatcher测试引发的事件

[英]testing for raised events by FileSystemWatcher

Hi I am currently trying to test failure of a change to the config file, 嗨,我目前正在尝试测试配置文件更改失败,

I use the following code to do this... 我使用以下代码执行此操作...

string path = _pathInvalidFormat.Substring(0, _pathInvalidFormat.LastIndexOf('\\'));
System.IO.File.Copy("TestInvalidXmlConfigurationFile.xml", _pathInvalidFormat, true);
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path);

//catch the invalid file getting deleted
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);

//catch the temporary config file getting renamed
fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemWatcher_Renamed);
fileSystemWatcher.EnableRaisingEvents = true;

CreateConfig(_pathInvalidFormat);
System.Threading.Thread.Sleep(5000);
Assert.That(_oldFileDeleted);
Assert.That(_newFileCreated);
ResetConfig(_pathInvalidFormat);

However I am not happy with this use of System.Threading.Thread.Sleep(5000); 但是,我对使用System.Threading.Thread.Sleep(5000)感到不满意;

I have tried using fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted, 5000); 我尝试使用fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted,5000); but cant seem to get it to work, as it always just seems to reach the timeout. 但似乎无法使其正常工作,因为它似乎总是会超时。 Even when I can see the Deleted event has been hit, it still doesn't return. 即使我看到Deleted事件已被击中,它仍然不会返回。

Alternatively is there a better way of getting the system to wait for asynchronous events to be fired? 或者,是否有更好的方法让系统等待异步事件被触发?

Thanks 谢谢

Kieran 基兰

-- edited: -编辑:

I use createconfig(path) to create a system config file using the following method 我使用createconfig(path)通过以下方法创建系统配置文件

private void ReadConfiguration(string path)
{
    var configFileMap = new ExeConfigurationFileMap()
    {
        ExeConfigFilename = path,
    };

    // if we try to read bad formatted file let's
    // 1. delete this file
    // 2. call read configuration to form correct file
    try
    {
        Config = System.Configuration.ConfigurationManager
            .OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    }
    catch (ConfigurationErrorsException)
    {
        // ok, there is bad format file, let delete it and create another one
        // TODO: check security rights?
        File.Delete(path);
        ReadConfiguration(path);
    }
}

Hope this gives some insight as to what exactly my test is trying to achieve. 希望这能为我的测试试图达到的目标提供一些见识。

Put all the code in a function and execute it asynchronous with thread. 将所有代码放入函数中,并与线程异步执行。

Now, you can use manual reset events of threading to execute further code after sleep when file has been deleted, renamed and invalid path error checked. 现在,当文件被删除,重命名和检查无效路径错误后,您可以使用线程的手动重置事件在睡眠后执行更多代码。

In this manner, code execution will be not blocked. 这样,代码执行将不会被阻塞。

refer this link for example 请参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM