Parallel Processing/Parallel Programming in C#
I need to process more than one function that goes and performs results returning the same type of records. I am using c# under visual studio 2010 considering the functions i have开发者_如何学C are:
class Search{
public list<wrecords> GetrecordsofAAA(string term);
public list<wrecords> GetrecordsofBBB(string term);
public list<wrecords> GetrecordsofCCC(string term);
}
and im calling the functions this way
list<wrecords> records1 = Search.GetrecordsofAAA(heart);
list<wrecords> records2 = Search.GetrecordsofBBB(heart);
list<wrecords> records3 = Search.GetrecordsofCCC(heart);
this is series processing.
I need records1, records2 and records 3 to be filled simultaneously, if possible.
Take a look at the Task Parallel Library (TPL) which was introduced in .NET Framework 4 (ie. with Visual Studio 2010). More specifically, in terms of the TPL, your problem can be solved with task parallelism.
Now, I'm not an TPL expert myself at all, but documentation suggests that you should be able to do what you want with 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); }
);
P.S.: Once your methods execute in parallel, you need to make sure that none of them has any side effects that could result in the other methods not functioning properly. (For instance, if these methods read and modify the same static fields belonging to your Search
class, that could lead to problems.)
//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));
Using Task Parallel 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 garuntee that the operations will be asynchronous. If you need the garuntee, than use Tasks like above.
This is a sketch of a possible solution: Create tasks for each method you want to run, start each task and then WaitAll() to wait for every 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);
精彩评论