如何合併基於TPL的任務的List<T>
以便以後執行?如何合併兩個Linq IEnumerable <T>查詢而不運行它們?
public async IEnumerable<Task<string>> CreateTasks(){ /* stuff*/ }
我的假設是.Concat()
......
void MainTestApp() // Full sample available upon request.
{
List<string> nothingList = new List<string>();
nothingList.Add("whatever");
cts = new CancellationTokenSource();
delayedExecution =
from str in nothingList
select AccessTheWebAsync("", cts.Token);
delayedExecution2 =
from str in nothingList
select AccessTheWebAsync("1", cts.Token);
delayedExecution = delayedExecution.Concat(delayedExecution2);
}
/// SNIP
async Task AccessTheWebAsync(string nothing, CancellationToken ct)
{
// return a Task
}
我要確保這不會產卵任何任務或評估任何東西。實際上,我想我問的是「什麼邏輯上執行IQueryable返回數據的東西」?
背景
因爲我在做遞歸,我不希望執行,直到正確的時間,究竟是合併,如果多次調用的結果正確的方法是什麼?
如果它的事項,我想運行此命令來啓動,然後該代碼的所有任務var AllRunningDataTasks = results.ToList();
的:
while (AllRunningDataTasks.Count > 0)
{
// Identify the first task that completes.
Task<TableResult> firstFinishedTask = await Task.WhenAny(AllRunningDataTasks);
// ***Remove the selected task from the list so that you don't
// process it more than once.
AllRunningDataTasks.Remove(firstFinishedTask);
// TODO: Await the completed task.
var taskOfTableResult = await firstFinishedTask;
// Todo: (doen't work)
TrustState thisState = (TrustState)firstFinishedTask.AsyncState;
// TODO: Update the concurrent dictionary with data
// thisState.QueryStartPoint + thisState.ThingToSearchFor
Interlocked.Decrement(ref thisState.RunningDirectQueries);
Interlocked.Increment(ref thisState.CompletedDirectQueries);
if (thisState.RunningDirectQueries == 0)
{
thisState.TimeCompleted = DateTime.UtcNow;
}
}
爲什麼concat不工作,它應該工作?另外,你不想運行任務,但運行查詢很好,Rght? – Tilak
@Tilak我的工作重點是任務,這是我第一次在任務或查詢中進行這項工作。我從來沒有這樣做過查詢,但我記得讀過Concat是如何完成的。 – LamonteCristo
@Tilak也許我在我的代碼中發現了一個錯誤...將很快更新 – LamonteCristo