78949

Call methods parallel and combine results

Question:

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.

Answer1:

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);

Answer2:

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:

<ul><li><a href="https://msdn.microsoft.com/en-us/library/hh194874(v=vs.110).aspx" rel="nofollow">Task.WhenAll</a></li> <li><a href="https://msdn.microsoft.com/en-us/library/hh194921(v=vs.110).aspx" rel="nofollow">Task.Run</a></li> <li><a href="https://msdn.microsoft.com/en-us/library/bb534336(v=vs.100).aspx" rel="nofollow">Enumerable.SelectMany</a> (as an extension method)</li> </ul>

Recommend

  • `numpy.diff` and `scipy.fftpack.diff` giving different results when differentiating
  • Watson Conversation - Retrieving specific data from nested context
  • Hash Function with Order Preserving
  • CVS command to get the current revision number of file
  • Some floating point precision and numeric limits question
  • How to validate a url with lots of querystrings
  • Get Users in Group from Azure AD via Microsoft Graph
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • Run EF6 Query in separate Thread on WinForm Button Click Event
  • Exception HRESULT: 0x800455BC in speech recognition in Windows phone 8
  • How to create two column output from a single column
  • is there a way to update filter with async data
  • Authentication failed with Azure Active Directory in Windows Phone
  • In loopback documentation what does variable 'cb' stands for?
  • ThreadStatic in asynchronous ASP.NET Web API
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • Allowing both email and username for authentication
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal