c# - UnhandledExceptionMode.ThrowException for AppDomain.UnhandledException -
I want to set unhandledExceptionMode.ThrowException for current AppDomain and not only for application threads. An exception from the second thread should also be handled by my current Donan_Unimated exception handler. Is it possible? Here's my code:
Fixed class program {/// & lt; Summary & gt; Main entry point for the application /// & lt; / Summary & gt; [STAThread] static zero major () {applications Set unhandled exception mode (unwanted exception. Throws up); AppDomain.CurrentDomain.UnhandledException + = New UnrestrictedExceptionEndception (CurrentDomain_UnhandledException); Application.EnableVisualStyles (); Application.SetCompatibleTextRenderingDefault (wrong); Application.Run (New Form 1 ()); } Static Zero CurrentDomain_UnhandledException (Object Sender, UnhandledExceptionEventArgs E) {File.AppendAllText ("Log.txt", (Exception) e.ExceptionObject). Message + environment. Newline); }}
Maybe you search for the behavior provided by Application.ThreadException Are
?
According to your comment, you want to have a way of achieving the behavior, enable the unrestricted exception policy attached to the net. 1.
In App.config:
& lt; Runtime & gt; & Lt; LegacyUnhandledExceptionPolicy enabled = "1" /> & Lt; / Order & gt;
) {Try {AppDomain.CurrentDomain.UnhandledException + CurrentDomain_UnhandledException; ThreadPool.QueueUserWorkItem (commodity object) = & gt; {New exception throw ("throw exceptions on background thread");}); Throw a new exception ("exception at the end of the menu"); } Grip (exception exception) {// throwing exception at main thread; Hondol from here will be exception exception (exception); }} Fixed zero Existing Demon_additional exception (Object Sender, UnhandledExceptionEventArgs E) {// Exception throwing on background threads will pass here HandleUnhandledException (Exception) e.ExceptionObject); } Fixed Zero Handleadded exception (exception exception) {// TODO: Regularly write your console unregistered exception here. WrititeLine (Exception Message); }}
Legacy exception policy causes exception to ignoring main threads on background threads. They will only be seen by the exception listener without exception exception. Exceptions on the main thread need to be controlled to prevent NAT unchecked exception dialog, but, using a shared method, you can reuse the exception handling code.
The event headler that you attach to AppDomain.UnhandledException
capture all the uncontrolled exceptions which AppDomain
Are generated on all threads in. If you delete a Windows-form from the picture, it shows some code that shows it:
class program {[STAThread] static zero main (string [] args] {app dotman . ContentDamman.United Express + = CurrentDemon_Entrue exceptions; ThreadPool.QueueUserWorkItem (commodity object) = & gt; {New exception throw ("throw exceptions on background thread");}); Throw a new exception ("exception at the end of the menu"); } Static void CurrentDomain_UnhandledException (Object Sender, UnhandledExceptionEventArgs E) {Console.WriteLine (Exception) e.ExceptionObject). message); }}
If you run it, you will see that both the exception message console is written immediately after the application crash due to the unhandled exception of the main
. ( Note : Indicates that event handler works for both exceptions, but in its actual position there is a race condition, an uncontrolled exception before AppDomain
will destroy the next one. )
Comments
Post a Comment