parallel processing - Task example in C# -
i starting using tasks in c#. trying execute code.
private static void createsubtask() { task<int32[]> parent = task.run(() => { var results = new int32[3]; new task(() => results[0] = 0, taskcreationoptions.attachedtoparent).start(); new task(() => results[1] = 1, taskcreationoptions.attachedtoparent).start(); new task(() => results[2] = 2, taskcreationoptions.attachedtoparent).start(); return results; }); var finaltask = parent.continuewith( parenttask => { foreach (int in parenttask.result) console.writeline(i); }); finaltask.wait(); } the finaltask runs after parent task finished, , parent task finishes when 3 children finished. can use create quite complex task hierarchies go through steps specified.
what got execution instead 3 lines saying:
0 0 0
i expecting them be
0 1 2
am right?
using task.run parent task suppresses effect of attachedtoparent on teh child task: http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx
use task.factory.startnew instead.
Comments
Post a Comment