避免在ui线程执行Parallel更新ui,否则会导致ui deadlocks

                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);
                });
            });

 

posted on 2021-06-18 17:24  Shine-Zhong  阅读(101)  评论(0编辑  收藏  举报

导航