c# - Catch TargetInvocationException inside a Task.Factory.StartNew(() => -
i'm developing windows forms application c#, .net framework 4.0 , visual studio 2012 premium.
i have method:
private void firstphasebtn_click(object sender, eventargs e) { var task = task.factory.startnew(() => { if (_viewmodel == null) _viewmodel = new mainviewmodel(); this.begininvoke((action)delegate() { labelloading.text = "creando orden..."; labelloading.visible = true; models.firstphasemodel model = new models.firstphasemodel() { // set data. }; ordernumberlabel.text = _viewmodel.firstphase(model); firstphasebtn.enabled = true; labelloading.text = string.empty; labelloading.visible = false; }); }); try { task.wait(); } catch (exception) { messagebox.show(this, _viewmodel.clientcustomerror, "error"); } } on _viewmodel.firstphase(model); http request web service.
my problem here try catch block doesn't work. unhandled exception.
i have tried run project in debug, release, , running executable file on release folder, unhandled exception.
i have tried put try catch block inside task, same result.
how can handled exception in task?
i have tried add this:
.continuewith(t => { labelloading.visible = false; labelloading.text = string.empty; messagebox.show(this, _viewmodel.clientcustomerror, "error"); }, taskcontinuationoptions.onlyonfaulted); but i'm still getting system.reflection.targetinvocationexception.
problem here:
this.begininvoke((action)delegate() { labelloading.text = "creando orden..."; labelloading.visible = true; models.firstphasemodel model = new models.firstphasemodel() { // set data. }; ordernumberlabel.text = _viewmodel.firstphase(model); firstphasebtn.enabled = true; labelloading.text = string.empty; labelloading.visible = false; }); once call begininvoke, you're running things on ui thread , out of task in turn, means tpl no longer able track , trace exceptions , combining them on aggregateexception.
this easier way demonstrate problem:
public partial class form1 : form { public form1() { initializecomponent(); } protected override void onshown(eventargs e) { base.onshown(e); var task = task.factory.startnew(() => { thread.sleep(1000); begininvoke((action)delegate { throw new notimplementedexception(); }); }); try { task.wait(); } catch (exception ex) { console.writeline(ex.message); } } } if run code, crash application (unless you've subscribed relevant catch exception handler can't remember anymore).
what need either move begininvoke code out (using task continuation on correct scheduler) or try/catching entirety of code inside begininvoke().
update:
this how rewrite code:
var task = task.factory.startnew(() => { if (_viewmodel == null) { _viewmodel = new mainviewmodel(); } }). continuewith(x => { labelloading.text = "creando orden..."; labelloading.visible = true; models.firstphasemodel model = new models.firstphasemodel() { // set data. }; ordernumberlabel.text = _viewmodel.firstphase(model); firstphasebtn.enabled = true; labelloading.text = string.empty; labelloading.visible = false; }, taskscheduler.current).continuewith(result => { if (result.isfaulted) { // result , "consume" it. _log.error(result.exception); } });
Comments
Post a Comment