0
下面的代碼有什麼問題? Task.Start失敗,索引超出範圍例外。要更清楚..它是失敗的,因爲我的價值是作爲3循環中的!當繼續操作未內聯時,Task.Start失敗
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(); });
for (int i = 0; i < actions.Count; i++)
{
t.ContinueWith(t1 => actions[i]());
}
t.Start();
確切地說 - 有關更多信息,請參閱http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx – 2011-04-29 08:38:22
修復這個問題..謝謝 – user730702 2011-04-29 09:02:24
如果我修改每個代碼使用,有趣的是這個代碼執行三次foreach(var item in actions) t.ContinueWith(t1 => item()); } – user730702 2011-04-29 09:04:55