2

I have a MainMethod which needs to call two methods Method1 and Method2 parallel. Both of them will return list of Employee but from different database. I need to call them parallel and then combine the results of Method1 and Method2 in MainMethod and then return result to the caller of MainMethod.

I greatly appreciate if people can tell what must be signatures of methods and what code I need to write I mean async/await keywords.

Servy
  • 202,030
  • 26
  • 332
  • 449
Ziggler
  • 3,361
  • 3
  • 43
  • 61
  • 2
    Why do you "need" them parallel? Here's one duplicate: http://stackoverflow.com/questions/7320491/simplest-way-to-run-three-methods-in-parallel-in-c-sharp – D Stanley May 15 '15 at 13:58
  • 1
    @DStanley - Parallel.Invoke is not the way to collect results. – H H May 15 '15 at 14:04
  • @DStanley Nor is it guaranteed to run in parallel. – Magnus May 15 '15 at 14:08
  • The code is not garuanteed to run at all - make sure that if you make tasks using `new Task` to call `Task.Start`. Also, since you lock while performing the get method, the long running parts will likely _not_ run in parallel, though it will get a proper result. – David May 15 '15 at 16:16
  • @Aravol . : I updated my question/code. I just want to lock so that only one thread access ints to add results. Is that wrong? – Ziggler May 15 '15 at 16:25
  • 1
    This lock is technically correct in that it prevents multiaccess to the results. However, as both my and @HenkHolterman do in our answerrs, it's better for each task to run, have result, and then concatenate those results, instead of adding to a singularized buffer inline. Less locking, fewer gotchas (if your query returns an `IEnumerable<>`, it may not actually execute until iterated,and this still do it's work in the lock). – David May 15 '15 at 16:34
  • You should not be editing the answer into the questions. Answers belong in the answers. – Servy May 19 '15 at 15:07
  • I updated my question and pasted my code. Thanks to Henk – Ziggler May 19 '15 at 15:07

2 Answers2

3

Using a bit more shorthand...

public static async Task<IEnumerable<Employee>> MainMethod()
{
    // Await when all to get an array of result sets after all of then have finished
    var results = await Task.WhenAll(
        Task.Run(() => Method1()), // Note that this leaves room for parameters to Method1...
        Task.Run(Method2)          // While this shorthands if there are no parameters
        // Any further method calls can go here as more Task.Run calls
        );

    // Simply select many over the result sets to get each result
    return results.SelectMany(r => r);
}

for signature reference, this uses the following .NET functions:

David
  • 10,458
  • 1
  • 28
  • 40
  • Can you please tell me what is signature of Method1() and Method2(). Can you please write full code. – Ziggler May 19 '15 at 00:07
  • `Method1` and `Method2` are simply examples, taken from the original writing of this question. I can add a signatyure, though – David May 19 '15 at 11:49
2

You can run them as 2 Task<T>s. The Result property takes care of the waiting. Approximately:

// untested 
Task<List<Employee>> t1 = Task.Factory.StartNew(() => Method1());
Task<List<Employee>> t2 = Task.Factory.StartNew(() => Method2());

var result = t1.Result.Concat(t2.Result);
H H
  • 263,252
  • 30
  • 330
  • 514
  • Henk... can you please tell me what must be signature of Method1() or Method2() and how I can return results from them and any async/await keywords I need to use. – Ziggler May 15 '15 at 15:37
  • They just need to return `List`. No await here. – H H May 15 '15 at 15:46
  • Henk.. please see my updates question. I added a sample code that I wrote. Is that correct? – Ziggler May 15 '15 at 16:13
  • Yes it looks correct but neither short nor efficient. What went wrong with the Task approach? – H H May 15 '15 at 17:08
  • I donot know why my question was edited. It clearly has the complete code what I wrote so that others can use it. But this is the signature of my methods private static List Method1() and private static List Method2() – Ziggler Jan 14 '16 at 19:19
  • Servy regarded it an answer. You intended it as a follow-up but it's different enough to be a separate question. We like questions short and on topic here, not long threads. – H H Jan 15 '16 at 07:47