2016-04-28 63 views
1

我有一個看起來像這樣的代碼:運行多個等候在同一時間

firstList = await GetFirstListFilesAsync(); 
textBlock1.Text = "found " + firstList.Count + " first list's results"; 
secondList = await GetSecondListFilesAsync(); 
textBlock2.Text = "found " + secondList.Count + " second list's results"; 
thirdList = await GetThirdListFilesAsync(); 
textBlock3.Text = "found " + thirdList.Count + " third list's results" 

所以,現在它被第一個列表,將第一TextBlock文本,然後它得到第二個列表,設置第二TextBlock文字和然後獲取第三個列表並設置第三個TextBlock文本。但我希望所有等待的操作都能同時運行,因此所有TextBlock s都會同時更新或更新。或者可能不是在同一時間 - 每個TextBlock會隨着相應的等待方法完成而更新。無論如何,我想在這裏實現的目標是更快地獲得結果。如果我一個接一個地運行這些方法,他們會一直等到前一個結束,但如果它們同時運行,第三個方法會更快地返回結果,對嗎?所以我的問題是 - 這可能嗎?如果是,那麼如何?

+0

創建一個新的功能,並把GetFirstListFilesAsync,GetSecondListFilesAsync,GetThirdListFilesAsync和返回的所有邏輯int和List <>的數組或字典,並使用await運行該函數。 – progrAmmar

回答

4

我認爲這是你在找什麼:

var tasks = new Task<MyMethodReturnObject>[3]; 
tasks[0] = GetFirstListFilesAsync(); 
tasks[1] = GetSecondListFilesAsync(); 
tasks[2] = GetThirdListFilesAsync(); 

// At this point, all three tasks are running at the same time. 

// Now, we await them all. 
await Task.WhenAll(tasks); 

// Get an individual result, where result is MyMethodReturnObject 
var myText = tasks[0].Result; 

// Loop through results 
var results = tasks.Select(o => o.Result).ToList(); 
foreach (var result in results) 
{ 
    Console.Write(result); 
} 

來源:http://blog.stephencleary.com/2012/02/async-and-await.html

+0

好的,但我怎樣才能使用此分配結果列表? – dace

+0

我的語法可能稍微偏離這裏,因爲我現在沒有訪問VS,但是像這樣: 'code' var results = tasks.Select(o => o.Result).ToList(); foreach(var result in results) { //對結果做些什麼 - result.Count; }'code' – jondow

+0

非常好的回答@jondow。 @dace只是做'firstList = tasks [0] .Result;' – Stamos

1

您可以嘗試將工作轉移到Task的集合中,並使用Task.WaitAll來加入線程。

+0

或者像把所有的等待放在一起那麼簡單,然後像這樣調用設置的文本值: firstList = await GetFirstListFilesAsync(); secondList = await GetSecondListFilesAsync(); thirdList = await GetThirdListFilesAsync(); textBlock1.Text =「found」+ firstList.Count +「first list's results」; textBlock2.Text =「找到」+ secondList.Count +「第二個列表的結果」; textBlock3.Text =「找到」+ thirdList.Count +「第三個列表的結果」 –

1

使用大衛的回答還是一樣把所有的等待在一起,然後調用設置文本值這樣簡單:

firstList = await GetFirstListFilesAsync(); 
secondList = await GetSecondListFilesAsync(); 
thirdList = await GetThirdListFilesAsync(); 

textBlock1.Text = "found " + firstList.Count + " first list's results"; 

textBlock2.Text = "found " + secondList.Count + " second list's results"; 

textBlock3.Text = "found " + thirdList.Count + " third list's results" 
+0

這仍然不會並行運行任務。 'GetSecondListFilesAsync'只有等到GetFirstListFilesAsync產生完成後纔會被調用。 –

-1

只需將創建爲即可他們:

var firstWait = GetFirstListFilesAsync(); 
var secondWait = GetSecondListFilesAsync(); 
var thirdWait = GetThirdListFilesAsync(); 
firstList = await firstWait; 
textBlock1.Text = "found " + firstList.Count + " first list's results"; 
secondList = await secondWait ; 
textBlock2.Text = "found " + secondList.Count + " second list's results"; 
thirdList = await thirdWait ; 
textBlock3.Text = "found " + thirdList.Count + " third list's results"; 

這仍然不會顯示任何結果,直到第一項已完成,但允許並行地取得進展。如果你想處理每一個作爲它在一個單一的功能內完成,因爲你必須WaitAny關於awaitables的集合,然後確定(手動)哪一個已完成,從那裏刪除那些收集和循環,直到所有完成。

0

所以你不想分組await塊,你想要指定完成後會發生什麼。

GetFirstListFilesAsync().ContinueWith(task => { 
    textBlock1.Text = "found " + task.Result.Count + " first list's results"; 
}); 
GetSecondListFilesAsync().ContinueWith(task => { 
    textBlock2.Text = "found " + task.Result.Count + " second list's results"; 
}); 
GetThirdListFilesAsync().ContinueWith(task => { 
    textBlock3.Text = "found " + task.Result.Count + " third list's results"; 
}); 

這樣會做到這一點的最快方法,因爲一旦Task完成後,您textBlock.Text將被設置,他們將不會在上一代人來完成等待。

0

我希望所有等待的操作都會同時運行,所以所有的TextBlocks都會同時更新或更新。或者可能不在同一時間 - 每個TextBlock將會隨着相應的await方法完成而更新。

然後定義了一些新的方法:

private async Task<TFirst> GetFirstListFilesAndDisplayAsync() 
{ 
    var firstList = await GetFirstListFilesAsync(); 
    textBlock1.Text = "found " + firstList.Count + " first list's results"; 
} 

private async Task<TSecond> GetSecondListFilesAndDisplayAsync() 
{ 
    var secondList = await GetSecondListFilesAsync(); 
    textBlock2.Text = "found " + secondList.Count + " second list's results"; 
} 

private async Task<TThird> GetThirdListFilesAndDisplayAsync() 
{ 
    var thirdList = await GetThirdListFilesAsync(); 
    textBlock3.Text = "found " + thirdList.Count + " third list's results"; 
} 

然後同時打電話給他們所有:

var firstTask = GetFirstListFilesAndDisplayAsync(); 
var secondTask = GetSecondListFilesAndDisplayAsync(); 
var thirdTask = GetThirdListFilesAndDisplayAsync(); 

await Task.WhenAll(firstTask, secondTask, thirdTask); 

firstList = await firstTask; 
secondList = await secondTask; 
thirdList = await thirdTask; 
相關問題