没有断点时,FileSystemWatcher通知崩溃

时间:2011-06-28 14:59:55

标签: c# .net visual-studio-2010

好的,这很奇怪。

我创建了一个应用程序,用于侦听文件中的更改,然后将这些更改复制到另一个文件。一切都运转正常,我几乎什么都没改变,试图回滚我的改变,但仍然没有运气。

所以这就是问题:我有一个FileSystemWatcher,以及一个在“Changed”事件中调用的方法。一段时间以来,我注意到我的应用程序在发生变化时崩溃,所以我查看了我的方法,一切都很好。事实上,当我在其中放置断点时它会运行。但是,如果我删除了断点,那么它会崩溃,在输出中不会出现令人担忧的错误(我认为),而不是:

The thread 'vshost.NotifyLoad' (0xa84) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x17e8) has exited with code 0 (0x0).
'TimeDataDuplicator.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Tommy Bergeron\Documents\Visual Studio 2010\Projects\vs-projects\TimeDataDuplicator\TimeDataDuplicator\bin\Debug\TimeDataDuplicator.exe', Symbols loaded.
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
The program '[6240] TimeDataDuplicator.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6240] TimeDataDuplicator.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

当没有断点时,一切都运行正常是非常奇怪的......我尝试清理我的解决方案和我的项目,但没有任何效果。

有什么想法吗?有没有人偶然发现这个?

非常感谢!


以下是观察者:

private void StartWatcher()
{
    FileSystemWatcher watcher = new FileSystemWatcher();

    // Répertoire
    watcher.Path = @"C:\Symcod_data";

    // Nom du fichier à "watcher"
    watcher.Filter = "FILE_MSG.OK";

    // Choix des événements à notifier
    watcher.NotifyFilter = NotifyFilters.Size;

    // Assignation de méthode pour les événements choisies.
    watcher.Changed += FileHasChanged;

    // Début de l'observation
    watcher.EnableRaisingEvents = true;

    WriteLogMessage("TDD_WATCH_STARTED");
}

这是被调用的方法:

void FileHasChanged(object source, FileSystemEventArgs e)
{
    // Lecture du fichier original
    using (StreamReader fileReader = new StreamReader(originalFile))
    {
        // On garde en mémoire le contenu du fichier dupliqué
        StreamReader duplicatedCheckReader = new StreamReader(duplicatedFile);
        string duplicatedCheckContent = duplicatedCheckReader.ReadToEnd();
        duplicatedCheckReader.Close();

        // Traitement du contenu du fichier original
        string line;
        string middleLineContent;   // Contiendra seulement le millieu de la ligne (sans les caractères spéciaux)
        while ((line = fileReader.ReadLine()) != null)
        {
            // On se sert seulement que le millieu de la ligne pour faire la recherche des doublons
            middleLineContent = line.Substring(line.IndexOf("-") + 1, 16);

            // Vérification des doublons, si le doublon n'est trouvé on écrit dans le fichier
            if (!Regex.IsMatch(duplicatedCheckContent, middleLineContent))
            {
                // Initialisation de l'écriture
                try
                {
                    using (StreamWriter fileWriter = new StreamWriter(duplicatedFile, true))
                    {
                        fileWriter.WriteLine(line);
                    }

                    WriteLogMessage("WriteLine_SUCCESS (" + line + ")");
                }
                catch (Exception ex)
                {
                    WriteLogMessage("WriteLine_FAIL [Exception: " + ex.InnerException + "] (" + line + ")");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为问题是您的两个StreamReader或StreamWriter之一。我假设您尝试访问仍被其他进程锁定的文件,甚至是您自己的进程。

尝试尝试捕获事件处理程序的完整代码。这将显示异常发生在您的事件处理程序中而不是其他地方。