2011-09-02 39 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,比使用上面的任務更好。