我正在尝试找出一种方法来捕获抛出的异常FileSystemWatcher
正如我从软件的崩溃报告日志中注意到的那样,这些似乎是随机发生的。崩溃并不频繁,因为上个月只发生了两次,但很烦人,我很想修复它。有问题的异常似乎与路径中包含无效字符的文件有关。我不确定情况是否如此,或者引发的事件是否格式错误。到目前为止,我所知道的是异常的堆栈跟踪:
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)
从堆栈跟踪中可以清楚地看出,在将回调事件引发到我的应用程序之前,异常是在侦听器的执行上下文中引发的。我想知道是否有办法捕获这样的异常并继续执行,忽略该事件。
我尝试将观察者回调的主体封装为try/catch
块,但似乎执行永远不会到达回调,这真的令人沮丧,因为我现在开始认为这是 .Net 框架中的一个错误
您是否尝试过注册 OnError 事件?
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.onerror%28v=vs.110%29.aspx
EDIT:
唯一剩下的就是 ThreadException 和 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)