一個利用 Parallel.For 並行處理任務,帶有進度條(ProgressBar)的 WinForm 實例(上)

代碼很簡單: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

相關文章
相關標籤/搜索