這裏的主要思想是從某處獲取一些數據,當它被提取時開始寫入數據,然後準備下一批要寫入的數據,同時等待先前的數據寫完成。c#等價於用某些參數重新啓動任務
我知道一個Task
無法重新啓動或重新使用(也不應該),雖然我試圖找到一種方法,做這樣的事情:
//The "WriteTargetData" method should take the "data" variable
//created in the loop below as a parameter
//WriteData basically do a shedload of mongodb upserts in a separate thread,
//it takes approx. 20-30 secs to run
var task = new Task(() => WriteData(somedata));
//GetData also takes some time.
foreach (var data in queries.Select(GetData))
{
if (task.Status != TaskStatus.Running)
{
//start task with "data" as a parameter
//continue the loop to prepare the next batch of data to be written
}
else
{
//wait for task to be completed
//"restart" task
//continue the loop to prepare the next batch of data to be written
}
}
任何建議表示讚賞!謝謝。我不一定要使用Task
,我只是覺得它可能是要走的路。
這實際上是我正在尋找的!謝謝。我只需要找到一種方法來保持應用程序使用的內存不會隨着'while'循環而上升。 – LaurentH