c# - Using TaskScheduler.UnobservedTaskException can avoid the process being killed? -
as know, if there's unhandled exception in task, , if don't handle taskscheduler.unobservedtaskexception, error later when gets collected/finalized, , kill process.(i should emphasize in .net4.0. know can suppress unhandled exception in .net4.5 default.)
this means if use taskscheduler.unobservedtaskexception, application can survived? tested below code:
static void main(string[] args) { taskscheduler.unobservedtaskexception += (o, ev) => { console.writeline(ev.exception); console.writeline("---------"); }; (int = 0; < 10; i++) { try { var t = task.factory.startnew<int>(() => { throw new exception("xxxxxx"); return 1; } , cancellationtoken.none, taskcreationoptions.none, taskscheduler.default); } catch { console.writeline("error"); } } while (true) { gc.collect(); thread.sleep(1000); } console.readkey(); }
create 10 tasks , each task throw unhandled exception. , unobservedtaskexception triggered once. application broken. (i tried create 1 task, application isn't broken.)
so conclusion using unobservedtaskexception event no useful, process still can killed. right??
you need call unobservedtaskexceptioneventargs.setobserved
make observed exception:
taskscheduler.unobservedtaskexception += (o, ev) => { console.writeline(ev.exception); console.writeline("---------"); ev.setobserved(); };
Comments
Post a Comment