c# - Task.Start fails when continuation actions are not inline -
what wrong following code? task.start fails index out of range exception. more clear.. failing because value coming 3 in loop !!!
actionprovider m1 = new actionprovider(); actionprovider m2 = new actionprovider(); actionprovider m3 = new actionprovider(); list<action> actions = new list<action>() { ()=> { m2.doit(); }, ()=> { m3.doit(); }, }; task t = new task(() => { m1.doit(); }); (int = 0; < actions.count; i++) { t.continuewith(t1 => actions[i]()); } t.start();
it because reuse same variable, i, several times. when execute task has been incremented.
try change for-loop follows:
(int = 0; < actions.count; i++) { var action = actions[i]; t.continuewith(t1 => action()); }
the difference here create copy of variable pass continuewith.
Comments
Post a Comment