ProgressForm
2012-01-16 19:00 【當(dāng)耐特】 閱讀(3134) 評(píng)論(3) 收藏 舉報(bào)簡(jiǎn)介
在winform項(xiàng)目中,我們常常需要彈出一個(gè)窗體顯示處理的進(jìn)度,All in one code中的progress不能滿足我們的需求,所以就有了ProgressForm.
ProgressForm對(duì)外開放了一個(gè)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,以便通知界面更改進(jìn)度條百分比和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);
}
使用方式
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("已經(jīng)完成10%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(20);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成20%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(30);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成30%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(40);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成40%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(50);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成50%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(60);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成60%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(70);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成70%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(80);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成80%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(90);
progressForm.NotifyProgressMessageChanged("已經(jīng)完成90%");
Thread.Sleep(2000);
progressForm.NotifyProgressChanged(100);
progressForm.NotifyProgressMessageChanged("全部處理完成!");
Thread.Sleep(2000);
return string.Empty;
}
}
這里返回值是string,我們可以返回任何類型TResult,以便兩個(gè)窗體之間的交互。



浙公網(wǎng)安備 33010602011771號(hào)