这是启动多个任务的示例代码
Task.Factory.StartNew(() =>
{
//foreach (KeyValuePair<string, string> entry in dicList)
Parallel.ForEach(dicList,
entry =>
{
//create and add the Progress in UI thread
var ucProgress = (Progress)fpPanel.Invoke(createProgress, entry);
//execute ucProgress.Process(); in non-UI thread in parallel.
//the .Process(); must update UI by using *Invoke
ucProgress.Process();
System.Threading.Thread.SpinWait(5000000);
});
});
.ContinueWith(task =>
{
//to handle exceptions use task.Exception member
var progressBar = (ProgressBar)task.AsyncState;
if (!task.IsCancelled)
{
//hide progress bar here and reset pb.Value = 0
}
},
TaskScheduler.FromCurrentSynchronizationContext() //update UI from UI thread
);
Task.Factory.StartNew()
当我们使用then启动多个任务时,我们可以使用.ContinueWith()
block 来确定每个任务何时完成。我的意思是每个任务完成时,ContinueWith 块触发一次。所以我只想知道 TPL 库中是否有任何机制。如果我开始使用 10 个任务,Task.Factory.StartNew()
那么我如何在 10 个任务完成后通知。请通过示例代码提供一些见解。