2015-12-28 3 views
0

Я хотел проверить, можно ли его уловить все неиспользуемые исключения в одном и том же месте.Использование AppDomain.UnhandledException в многопоточном окружении

Я видел, что есть AppDomain.UnhandledException.

Я использовал это так:

public void MainMethod() 
{ 
    AppDomain currentDomain = AppDomain.CurrentDomain; 
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); 

    new Thread()... 
} 

static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
{ 
    Exception e = (Exception)args.ExceptionObject; 
    LogFile.Log("MyHandler caught : " + e.Message); 
} 

Это работает, когда необработанное исключение происходит в основном потоке, но во внутренней резьбе оно не поймать.

Можно ли исключить исключения из всех потоков в одном и том же месте или я должен обернуть их на каждую нить?

ответ

0

Мы используем этот метод, попробуйте:

private static void SetupExceptionHandling() 
    { 
     // Add the event handler for handling UI thread exceptions to the event. 
     Application.ThreadException += ApplicationThreadException; 

     // Set the unhandled exception mode to force all Windows Forms errors to go through our handler. 
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 

     // Add the event handler for handling non-UI thread exceptions to the event. 
     AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; 

     // This AppDomain-wide event provides a mechanism to prevent exception escalation policy (which, by default, terminates the process) from triggering. 
     // Each handler is passed a UnobservedTaskExceptionEventArgs instance, which may be used to examine the exception and to mark it as observed. 
     TaskScheduler.UnobservedTaskException += TaskSchedulerUnobservedTaskException; 

     // Add the event handler for handling UI thread exceptions to the event. 
     Dispatcher.CurrentDispatcher.UnhandledException += DispatcherUnhandledException; 
    } 
+0

У меня нет пользовательского интерфейса, поэтому единственные актуальным является AppDomain.CurrentDomain.UnhandledException? Я уже использую его, но, как я написал, он не поймает другие потоки – omriman12