Winform异步等待控件简单实现

来源:互联网 发布:无效的json字符串 编辑:程序博客网 时间:2024/05/21 22:27

思路

  1. BaseForm类继承Form class:通过拓展BaseForm类添加控件,使用时继承BaseForm
  2. 等待控件作用:
    • 禁用主窗体控件;
    • 显示进度条控件
  3. 异步调用: 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();            }        });    }}