一个利用 Parallel.For 并行处理任务,带有进度条(ProgressBar)的 WinForm 实例(下)
2019-03-25 19:53 音乐让我说 阅读(633) 评论(0) 编辑 收藏 举报
接着上一篇:一个利用 Parallel.For 并行处理任务,带有进度条(ProgressBar)的 WinForm 实例(上)
直接贴代码了:
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormUI.UIForms { public partial class TaskParallelTestForm : Form { public TaskParallelTestForm() { InitializeComponent(); } IEnumerable<int> _data = Enumerable.Range(1, 100); Action _cancelWork; private void DoWorkItem( int[] data, int item, CancellationToken token, Action<int> progressReport, ParallelLoopState loopState) { // observe cancellation if (token.IsCancellationRequested) { loopState.Stop(); return; } // simulate a work item Thread.Sleep(500); // update progress progressReport(item); } private void btnStart_Click(object sender, EventArgs e) { // update the UI this.btnStart.Enabled = false; this.btnStop.Enabled = true; Action enableUI = () => { // update the UI this.btnStart.Enabled = true; this.btnStop.Enabled = false; this._cancelWork = null; }; Action<Exception> handleError = (ex) => { // error reporting MessageBox.Show(ex.Message); }; try { // prepare to handle cancellation var cts = new CancellationTokenSource(); var token = cts.Token; this._cancelWork = () => { this.btnStop.Enabled = false; cts.Cancel(); }; var data = _data.ToArray(); var total = data.Length; // prepare the progress updates this.progressBar1.Value = 0; this.progressBar1.Minimum = 0; this.progressBar1.Maximum = total; var syncConext = SynchronizationContext.Current; Action<int> progressReport = (i) => syncConext.Post(_ => this.progressBar1.Increment(1), null); // offload Parallel.For from the UI thread // as a long-running operation var task = Task.Factory.StartNew(() => { Parallel.For(0, total, (item, loopState) => DoWorkItem(data, item, token, progressReport, loopState)); // observe cancellation token.ThrowIfCancellationRequested(); }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); task.ContinueWith(_ => { try { task.Wait(); // rethrow any error } catch (Exception ex) { while (ex is AggregateException && ex.InnerException != null) ex = ex.InnerException; handleError(ex); } enableUI(); }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { handleError(ex); enableUI(); } } private void btnStop_Click(object sender, EventArgs e) { if (this._cancelWork != null) this._cancelWork(); } } }
运行截图:
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2014-03-25 谷歌正式发布Google APIs Client Library for .NET
2011-03-25 安装Apache的步骤
2011-03-25 安装MySQL的步骤
2011-03-25 配置phpMyAdmin的步骤
2011-03-25 安装PHP的步骤
2011-03-25 (转)连线:HTTPS的安全性更高,为何网络不全面普及它?