Parallel Processing / Parallel Programming in C #

I need to process more than one function that goes, and execute results that return records of the same type. I use C # under visual studio 2010 considering that the functions I have are of the form:

class Search{ public list<wrecords> GetrecordsofAAA(string term); public list<wrecords> GetrecordsofBBB(string term); public list<wrecords> GetrecordsofCCC(string term); } 

and im calling functions this way

 list<wrecords> records1 = Search.GetrecordsofAAA(heart); list<wrecords> records2 = Search.GetrecordsofBBB(heart); list<wrecords> records3 = Search.GetrecordsofCCC(heart); 

this is a series processing.

I need records 1, records2 and records 3, which will be filled at the same time, if possible.

+4
source share
4 answers
 //make a list of the search functions you want to call. var Searches = new List<string, Func<string, IEnumerable<wrecord>>> { a => GetrecordsofAAA(a), a => GetrecordsofBBB(a), a => GetrecordsofCCC(a), } //Execute them in parallel and collect the results as lists of lists of wrecord IEnumerable<IEnumerable<wrecord>> result = Searches.AsParallel().Select(a => a(heart)); 
+3
source

Take a look at the parallel task library (TPL) that was introduced in the .NET Framework 4 (i.e., with Visual Studio 2010). More specifically, from the point of view of TPL, your problem can be solved using the parallelism task .

Now I'm not a TPL expert at all, but the documentation suggests that you should do what you want, Parallel.Invoke :

 using System.Threading.Tasks; … List<wrecords> records1 = null; List<wrecords> records2 = null; List<wrecords> records3 = null; // ^ Initialize these to any default value to prevent a compile-time error. // The compiler cannot know that below delegates will actually be called, // so without the initialization you would soon get a "use of unassigned // variable" error. Parallel.Invoke( () => { records1 = Search.GetrecordsofAAA(heart); }, () => { records2 = Search.GetrecordsofBBB(heart); }, () => { records3 = Search.GetrecordsofCCC(heart); } ); 

PS: As soon as your methods are executed in parallel, you must make sure that none of them has side effects that can lead to the malfunctioning of other methods. (For example, if these methods read and modify the same static fields that belong to your Search class, this can lead to problems.)

+6
source

Using a parallel task library, you can do something like:

 list<wrecords> records1; lList<wrecords> records2; lList<wrecords> records3; Task task1 = Task.Factory.StartNew(() => records1 = Search.GetrecordsofAAA(heart)); Task task2 = Task.Factory.StartNew(() => records2 = Search.GetrecordsofBBB(heart)); Task task3 = Task.Factory.StartNew(() => records3 = Search.GetrecordsofCCC(heart)); Task.WaitAll(task1, task2, task3); // Wait for all three tasks to finish // Do stuff after 

Parallel.Invoke does not guarantee that operations will be asynchronous. If you need garuntee, than use Tasks as described above.

+2
source

This is a sketch of a possible solution. Create tasks for each method that you want to run, run each task, and then WaitAll () to wait for each task to complete.

  // Create an array of methods to run Func<object, List<WRecord>>[] methods = new[] { s => GetRecordsOfAAA((string) s), s => GetRecordsOfBBB((string) s), s => GetRecordsOfCCC((string) s) }; // Create an array of tasks Task<List<WRecord>>[] tasks = new Task<List<WRecord>>[methods.Length]; // Create and start each task for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task<List<WRecord>>.Factory.StartNew(methods[i], heart); } // Wait for all tasks to complete. Results are in tasks[n].Result Task.WaitAll(tasks); 
+1
source

All Articles