c# - How to handle the exception of BackgroundWorker in the main process? -
i want handle unhandled exceptions in progress class, in wrote code error logging:
static class program { /// <summary> /// main entry point application. /// </summary> [stathread] static void main() { try { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); application.run(new loginpoint()); } catch (exception myexception) { //log unhandled exceptions. } } } but exceptions in backgroundworker not being handled correctly:
private void backgroundworker1_dowork(object sender, doworkeventargs e) { backgroundworker worker = sender backgroundworker; throw new exception("test exception!"); } private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e) { if (e.error != null) { throw new exception("i got exception"); } } i want exception "i got ..." handled in progress class, system's exception dialog appears when run (not debug) application.
in program.cs this:
[stathread] static void main() { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); appdomain.currentdomain.unhandledexception += appdomain_unhandledexception; application.setunhandledexceptionmode(unhandledexceptionmode.throwexception); application.run(new form1()); } static void appdomain_unhandledexception(object sender, unhandledexceptioneventargs e) { messagebox.show(((exception)e.exceptionobject).message, "appdomain.unhandledexception"); environment.exit(-1); } your form code should work that, , should appdomain.unhandledexception messagebox in run-mode (ctrl+f5)
Comments
Post a Comment