代码很简单:html
public partial class TaskParallelTestForm01 : Form { public TaskParallelTestForm01() { InitializeComponent(); } string[] GenerateList() => new string[500]; void DoWork() { Thread.Sleep(50); } private void BtnRun_Click(object sender, EventArgs e) { var list = GenerateList(); progressBar1.Maximum = list.Length; Task.Run(() => Parallel.ForEach(list, item => { DoWork(); // Update the progress bar on the Synchronization Context that owns this Form. this.Invoke(new Action(() => this.progressBar1.Value++)); })); } }
效果图:this
下一篇(高级篇):一个利用 Parallel.For 并行处理任务,带有进度条(ProgressBar)的 WinForm 实例(下)spa
谢谢浏览!code
原文出处:https://www.cnblogs.com/Music/p/a-winform-instance-with-progressbar-for-parallel-processing-tasks-using-parallel-for-with-simple.htmlorm