zoukankan      html  css  js  c++  java
  • ProgressForm

    简介

    在winform项目中,我们常常需要弹出一个窗体显示处理的进度,All in one code中的progress不能满足我们的需求,所以就有了ProgressForm.

    image

    ProgressForm对外开放了一个DoExecute抽象方法,以便使用者override .

        public abstract class Action<TResult>
        {
            protected abstract TResult DoExecute(ProgressForm<TResult> progressForm);
     
            public TResult Execute(Form ownerForm, string title, string initialMessage, bool customProgress = false)
            {
                return ProgressForm<TResult>.ExecuteAction(ownerForm, new ExecuteActionDelegate<TResult>(DoExecute), title, initialMessage, customProgress);
            }
        }
     

    ProgressForm继承自IProgressDelegate,以便通知界面更改进度条百分比和Message .

    public partial class ProgressForm<TResult> : Form, IProgressDelegate

    接口定义:

        /// <summary>
        /// Public interface used to notify progress changes
        /// </summary>
        public interface IProgressDelegate
        {
            void NotifyProgressMessageChanged(string message);
            void NotifyProgressChanged(int percentage);
        }

    使用方式

    image

     
            private void testProgress_Click(object sender, EventArgs e)
            {
                TestClass t = new TestClass();
                t.Execute(this.ParentForm, "窗体名称", "Message", true);          
            }

    重写DoExecute

     
        public class TestClass : DeploymentTools.UserInterface.Actions.Action<string>
        {
            protected override string  DoExecute(ProgressForm<string> progressForm)
            {
                progressForm.NotifyProgressChanged(10);
                progressForm.NotifyProgressMessageChanged("已经完成10%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(20);
                progressForm.NotifyProgressMessageChanged("已经完成20%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(30);
                progressForm.NotifyProgressMessageChanged("已经完成30%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(40);
                progressForm.NotifyProgressMessageChanged("已经完成40%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(50);
                progressForm.NotifyProgressMessageChanged("已经完成50%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(60);
                progressForm.NotifyProgressMessageChanged("已经完成60%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(70);
                progressForm.NotifyProgressMessageChanged("已经完成70%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(80);
                progressForm.NotifyProgressMessageChanged("已经完成80%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(90);
                progressForm.NotifyProgressMessageChanged("已经完成90%");
                Thread.Sleep(2000);
                progressForm.NotifyProgressChanged(100);
                progressForm.NotifyProgressMessageChanged("全部处理完成!");
                Thread.Sleep(2000);
                return string.Empty;
            }
        }

    这里返回值是string,我们可以返回任何类型TResult,以便两个窗体之间的交互。

    ProgressForm下载

  • 相关阅读:
    [SCOI2013]火柴棍数字(背包)
    [NOI2015]品酒大会
    后缀数组小结
    [POI2009]Slw
    [POI2009]Wie
    [POI2008]账本BBB
    ant语法和规范
    使用Hudson进行持续集成
    gnu make
    可信执行环境(TEE)介绍
  • 原文地址:https://www.cnblogs.com/iamzhanglei/p/2324019.html
Copyright © 2011-2022 走看看