FileSystemWatcher防止调试器捕获异常
本文关键字:捕获异常 调试器 FileSystemWatcher | 更新日期: 2023-09-27 17:51:05
我今天都快把自己逼疯了。我已经使用FileSystemWatcher一段时间来捕获文件更改了。我的一个项目最初是在框架3.5上的,为了利用一些EF的东西,它被移到了4.0,这似乎影响了visual studio允许我调试利用FileSystemWatcher的代码的方式。下面是我遇到的问题的一个小例子(见watcher_Changed):
class Program
{
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher(@"C:'inputFolder'");
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
Console.WriteLine("Ready");
watcher.EnableRaisingEvents = true;
Thread.Sleep(System.Threading.Timeout.Infinite);
}
static void watcher_Changed(object sender, FileSystemEventArgs e)
{
//This exception here (just an example), does not get sent to the debugger, rather it goes to the
//console and then the application exits
throw new ArgumentException();
}
}
在向控制台抛出ArgumentException后,代码将总是为我关闭。在更复杂的情况下,这会给我带来一些主要的调试问题。
任何想法?
我认为这是因为Visual Studio没有配置为在抛出System.ArgumentException
时中断,而是在用户处理时中断。由于异常发生在由FileSystemWatcher
创建的线程中,它可能没有任何类型的顶级异常处理程序。
要更改调试设置,请进入Debug'Exceptions,然后展开Common Language Runtime Exceptions
并搜索例外。或者使用Find...
按钮。然后确保选中"抛出"复选框。请注意,通常情况下您可能不需要此选项,因为许多应用程序可能会正常处理异常。