Parallel.ForEach(source, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = CancellationToken.Token }, item => { //里面下载图片,更新到ui DownImage(item); });
上面代码会导致ui deadlocks,需要如下图代码避免在ui线程执行parallel
Task.Run(() => { Parallel.ForEach(source, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = CancellationToken.Token }, item => { //里面下载图片,更新到ui DownImage(item); }); });