简体   繁体   English

捕获FileSystemWatcher侦听器线程引发的异常

[英]Catching exceptions thrown by the FileSystemWatcher listener thread

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. 我试图找出一种方法来捕获FileSystemWatcher引发的异常,这些异常似乎是随机发生的,正如我从软件崩溃报告日志中注意到的那样。 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. 我想知道是否仍然有捕获此类异常并继续执行而忽略该事件的情况。

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 我尝试用try/catch块封装观察者回调的主体,但似乎执行从未到达回调,并且它确实令人沮丧,因为我现在开始认为这是.Net框架中的错误

Have you tried registering the OnError event? 您是否尝试过注册OnError事件?

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

EDIT: 编辑:

The only thing left is ThreadException and UnhandledException: 剩下的唯一东西是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);

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

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