簡體   English   中英

捕獲FileSystemWatcher偵聽器線程引發的異常

[英]Catching exceptions thrown by the FileSystemWatcher listener thread

我試圖找出一種方法來捕獲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

編輯:

剩下的唯一東西是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