捕获FileSystemWatcher侦听器线程引发的异常 [英] Catching exceptions thrown by the FileSystemWatcher listener thread

查看:232
本文介绍了捕获FileSystemWatcher侦听器线程引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找出一种方法来捕获 FileSystemWatcher 引发的异常,这些异常似乎是随机发生的,正如我从软件崩溃报告日志中注意到的那样。这次崩溃并不频繁,因为它上个月只发生了两次,但是很烦人,我很想修复它。有问题的例外似乎与路径中包含无效字符的文件有关。我不确定是这种情况还是引发的事件格式不正确。到目前为止,我所知道的只是异常的堆栈跟踪:

I am trying to figure out a way to catch exceptions thrown by FileSystemWatcher these seem to happen randomly as I have noticed from the crash reports logs for my software. The crash is not frequent as it only happened twice last month but its annoying and I would love to fix it. The exception in question seems to be related to files having invalid characters in their paths. I am not sure if that is the case or if the event raised is malformed. So far all I know is the stack trace of the exception:

Top-level Exception
Type:        System.ArgumentException
Message:     Illegal characters in path.
Source:      mscorlib

    Stack Trace: 
    at System.IO.Path.GetFileName(String path)

    at System.IO.FileSystemWatcher.MatchPattern(String relativePath)

    at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)

    at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes,   NativeOverlapped* overlappedPointer)

    at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

从堆栈跟踪中可以明显看出,在向我的应用程序引发回调事件之前,异常是在侦听器的执行上下文中引发的。我想知道是否仍然有捕获此类异常并继续执行而忽略该事件的情况。

From the stack trace it is clear that the exception is raised within the execution context of the listener before raising the callback events to my application. I was wondering if there is anyway to catch such an exception and continue execution, ignoring the event.

我尝试用 try / catch 块封装观察者回调的主体,但似乎执行从未达到回调及其真正令人沮丧的地方,因为我现在开始认为它是.Net框架中的错误

I tried encapsulating my watcher callbacks' body with try/catch blocks but it seems that the execution never reaches the callbacks and its really frustrating as I am now starting to think that it is a bug in the .Net framework

推荐答案

您尝试过吗注册OnError事件?

Have you tried registering the OnError event?

http://msdn.microsoft.com/zh-CN/library/system.io.filesystemwatcher.onerror%28v=vs.110%29.aspx

编辑:

剩下的唯一东西是ThreadException和UnhandledException:

The only thing left is ThreadException and UnhandledException:

    // Add handler to handle the exception raised by main threads
    Application.ThreadException += 
    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    // Add handler to handle the exception raised by additional threads
    AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

这篇关于捕获FileSystemWatcher侦听器线程引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆