开发者

FileSystemWatcher Notification crash when no breakpoints

Okay, this is pre开发者_运维技巧tty weird.

I made an app that listens for changes inside a file, and then duplicate those changes to another file. Everything was working alright, I changed almost nothing, tried to rollback my changes but still no luck.

So here is the problem : I have a FileSystemWatcher, and a method that gets called on the "Changed" event. For some time now, I noticed that my app crashes when there's a change so I looked at my method and everything is just fine. In fact, it runs when I put a breakpoint inside it. But if I remove the breakpoint then it crashes, leaving no worrying errors (I think) in the output other than:

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

It is very weird that everything runs fine when there's no breakpoint... I tried cleaning my solution and my project but nothing worked.

Any ideas? Has anyone ever stumbled upon this?

Thanks a lot!


Here's the watcher:

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

And here's the method that gets called:

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 + ")");
                }
            }
        }
    }
}


I assume that the problem is one of your two StreamReaders or the StreamWriter. I assume you try to access a file that is still locked by some other process or even by your own process.

Try to put a try catch around the complete code of your event handler. This will show, that the exception is happening in your event handler and not someplace else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜