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下载

  • 相关阅读:
    mysql----show slave status G 说明
    mysqldump 的方式来搭建master-->slave 的复制架构
    C++----练习--string 从文件中一个一个单词的读直到文件尾
    python 全排列combinations和permutations函数
    什么是restful api
    git知识点
    Hash算法解决冲突的方法
    python之单例设计模式
    Linux常用命令大全
    SQLAlchemy中时间格式化及将时间戳转成对应时间的方法-mysql
  • 原文地址:https://www.cnblogs.com/iamzhanglei/p/2324019.html
Copyright © 2011-2022 走看看