Winform异步等待控件简单实现
来源:互联网 发布:无效的json字符串 编辑:程序博客网 时间:2024/05/21 22:27
思路
- BaseForm类继承Form class:通过拓展BaseForm类添加控件,使用时继承BaseForm
- 等待控件作用:
- 禁用主窗体控件;
- 显示进度条控件
- 异步调用: try{} finally{}进行控件的回收
实现
BaseForm
public partial class BaseForm : Form{ private ProgressBar progressBar = null; /// <summary> /// Show ProgressBarControl when waiting... /// </summary> public virtual ProgressBar ProgressBarControl { get { return this.progressBar; } set { this.progressBar = value; } } public BaseForm() { InitializeComponent(); }}
BaseFormEx
using System;using System.Collections.Generic;using System.Diagnostics;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace RoadmapSupporter{ public static class BaseFormEx { public static void BeginWait(this BaseForm baseForm) { Debug.Assert(baseForm != null); baseForm.Invoke((MethodInvoker)delegate { baseForm.Enabled = false; if (baseForm.ProgressBarControl == null) { baseForm.ProgressBarControl = new ProgressBar(); baseForm.Controls.Add(baseForm.ProgressBarControl); baseForm.ProgressBarControl.Size = new Size(246, 36); baseForm.ProgressBarControl.Name = "progressBar"; baseForm.ProgressBarControl.Visible = true; baseForm.ProgressBarControl.Style = ProgressBarStyle.Marquee; baseForm.ProgressBarControl.Location = new Point(baseForm.Width / 2 - baseForm.ProgressBarControl.Width / 2, baseForm.Height / 2 - baseForm.ProgressBarControl.Height); baseForm.ProgressBarControl.BringToFront(); } }); } public static void EndWait(this BaseForm baseForm) { Debug.Assert(baseForm != null); baseForm.Invoke((MethodInvoker)delegate { if (baseForm.ProgressBarControl != null) { if (baseForm.Controls.Contains(baseForm.ProgressBarControl)) baseForm.Controls.Remove(baseForm.ProgressBarControl); baseForm.ProgressBarControl.Dispose(); baseForm.ProgressBarControl = null; baseForm.Enabled = true; } }); } }}
Call
public partial class RoadmapSupporter : BaseForm{ public void paradigm{ this.BeginWait(); ThreadPool.QueueUserWorkItem(arg => { try { if (CompareInfo(_PublishingToolFileName, _PreviousFileName, ref _Epics)) { webBrowserShow.DocumentText = FormatEmail(_Epics); } else { Invoke((MethodInvoker)delegate { MessageBox.Show(this, "An Error occur when comparing epics", "Compare Epics"); }); } } finally { this.EndWait(); } }); }}
阅读全文
0 0
- Winform异步等待控件简单实现
- 异步委托实现多线程winform控件编程
- 异步委托实现多线程winform控件编程
- C#等待句柄实现异步
- Winform应用程序实现加载等待(C#)
- C#多线程异步访问winform中控件
- C#多线程异步访问winform中控件
- C#多线程异步访问winform中控件
- C#多线程异步访问winform中控件
- Winform 通用控件异步显示信息
- WinForm 控件 简单设置快捷键
- winform listbox控件简单使用。
- curl+exec实现异步无等待请求
- VB.NET实现任务异步等待功能
- 如何实现同步等待异步事件发生
- php异步实现,避免长时间等待
- 等待超时实现简单方法
- 异步等待
- 面试总结
- ACM括号配对问题
- Excel在统计分析中的应用—第八章—假设检验-总体方差未知且为大样本下的检验
- javawebday25(编码方式 获取资源路径)
- 图形学扫描线填充算法
- Winform异步等待控件简单实现
- MediaStore.Video.Thumbnails.getThumbnail获取本地视频缩略图
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- SVN| SVN常用命令
- 递归函数+python中的汉诺塔递归算法
- hduoj 6000 && 2016CCPC-final B. Wash(贪心)
- python 文件读写时用open还是codecs.open
- ACM街区最短路径问题
- 现代操作系统之进程与线程(下)