Winform异步等待控件简单实现

思路

  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();
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值