2011-09-02 47 views
4

我需要处理多个函数并执行返回相同类型记录的结果。 我使用C#在Visual Studio 2010中 考虑我具备的功能有:C#中的并行处理/并行编程

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

和IM调用函数这样

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

这是一系列的处理。

如果可能,我需要同时填写records1,records2和records3。

+0

更多的工作描述,而不是一个问题.... –

+0

查看任务类:http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx – FuleSnabel

+0

你有没有研究过线程/ TPL概念? – Zenwalker

回答

3
//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)); 
+1

嗯......我忍不住想你想在这里多说一点? –

+0

我不小心把帖子打到了早期:P – albertjan

+0

我觉得是那个或者一些流氓宠物干扰。 :) –

6

查看在.NET Framework 4(即使用Visual Studio 2010)中引入的Task Parallel Library (TPL)。更具体地说,就TPL而言,您的问题可以通过task parallelism来解决。

现在,我不是专家TPL自己所有,但文件暗示你应该能够做你想做的与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:一旦你的方法并行执行,你需要确保他们没有任何副作用,可能导致其他方法无法正常工作。 (例如,如果这些方法读取和修改属于您的类的相同静态字段,可能会导致问题。)

+0

我会试一试,尽快回复2你!非常感谢! – Ghassan

1

这是可能解决方案的草图:为每个要运行的方法创建任务,启动每个任务,然后WaitAll()等待每个任务完成。

 // 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); 
+0

实际上,由@stakx提出的Parallel.Invoke我认为是一种实用的方法,完全可以做到这一点,但是没有多少细节。 –

2

使用任务并行库,你可以这样做:

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不garuntee,该操作将是异步的。如果你需要Garuntee,比使用上面的任务更好。