代码改变世界

ProgressForm

2012-01-16 19:00  【当耐特】  阅读(3100)  评论(3编辑  收藏  举报

简介

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