2

I have a method that contains a series of method calls in it. Some of these method calls can occur at once and some have to be done sequentially. What is the most straight forward way to implement that in Task Parallel Library. All functions are calculation heavy and take less than a second to run. i.e.

public object MyMethod(InputClass myInput)
{
    var result = method1(myInput);
    var result1 = method2(result);
    var result2 = method3(result);
    var finalResult = method4(result1, result2);
    return finalResult;   
}

Method1 must be executed 1st, method2 + method3 can be executed in parallel, method4 must be executed last.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
PlTaylor
  • 7,345
  • 11
  • 52
  • 94

3 Answers3

4

You can use ContinueWith to assign sequential execution like below;

 TaskFactory myFactory = new TaskFactory();

 myFactory.StartNew(method).ContinueWith(delegate
 {
    Task t1 = myFactory.StartNew(method2);
    Task t2 = myFactory.StartNew(method3);

    myFactory.ContinueWhenAll(new [] {t1, t2}, method4);
 });
GregC
  • 7,737
  • 2
  • 53
  • 67
ertan
  • 645
  • 1
  • 7
  • 14
  • What would this achieve? Surely this still executes all methods in order. – trickdev Apr 14 '11 at 17:07
  • This code is written in continuation-passing style. Async blocks allow for code to flow in the same order as when you think about the problem. Compiler does the CPS conversion for you. – GregC Apr 14 '11 at 17:39
  • 2
    I understand CPS, but before the edit on this answer - it did not work. Also you must remember, while all tasks would now execute correctly the Thread it runs on would not block during execution - I don't know if this was a requirement of the OP though. – trickdev Apr 14 '11 at 18:23
  • you'd have to save off the task into local variable and wait for it. – GregC Apr 14 '11 at 18:34
  • 1
    It wasn't stated, but the entire method must block until it is all over. Method in original question has been edited to show that the finalResults has to be passed out. – PlTaylor Apr 14 '11 at 18:35
2

I believe this would give the the level of parallelism you require, with method 2 and 3 being executed concurrently.

public void MyMethod(InputClass myInput)
{
    TaskFactory<object> t = new TaskFactory<object>();

    var result = method1(myInput);  // Execute Synchronously

    Task<object> t1 = t.StartNew(method2, result); // Create and start new concurrent task
    Task<object> t2 = t.StartNew(method3, result); // Create and start new concurrent task

    t1.Wait(); //Wait for completion
    t2.Wait(); //Wait for completion

    var finalResult = method4(t1.Result, t2.Result);  // Execute Synchronously
}

EDIT: Updating with some types, assuming all your methods return object.

trickdev
  • 627
  • 1
  • 7
  • 14
1

Async blocks in F#, as well as the new Async CTP are designed to solve such problems cleanly and effectively, allowing for external configuration of the concurrent pipeline. I know you'd said TPL, but I do recommend you have a look.

async {
   let! result = method1 myInput
   let! result1 = method2 result
   let! result2 = method3 result
   let! finalResult = method4 result1 result2
}
GregC
  • 7,737
  • 2
  • 53
  • 67
  • This is completely off topic...but I have spent the past two years building a simulation program in C#. In comparison to previous 'languages' I have used (Matlab primarily) I am in a much happier spot now. Why would I consider F#, people keep bringing it up and I haven't figured out the benefit of it yet. Can you clue me in a little. – PlTaylor Apr 14 '11 at 18:10
  • 1
    Please see Brian's comment: http://stackoverflow.com/questions/2444676/understanding-f-asynchronous-programming – GregC Apr 14 '11 at 18:20
  • If you're coming from MatLab universe, you'll love F#, since it's built for scientific computing. I'd recommend a book as well: http://www.amazon.com/F-Scientists-Jon-Harrop/dp/0470242116/ref=sr_1_1?ie=UTF8&s=books&qid=1302805276&sr=8-1 – GregC Apr 14 '11 at 18:21
  • Those are good links, but maybe I just need to force myself to try it out before I really understand the benefits. – PlTaylor Apr 14 '11 at 18:42