有一个窗体A,上面有一个按钮 mybutton1 点击后会执行一段要运行要很长时间的函数“fun()”。
我想给它加上进度条。
思路是这样的。在fun()函数前打开B窗体上面放一个进度条。(专门用于放进度条,没有边框的)。等执行完fun()后,关闭B窗体。
步骤:
新建了一个窗体B。上面放了一个进度条控件"mypro"。
在load_B事件中写下如代码(代码大小写有错,这个忽略,我主要是写出我的思路)
form B 的代码:
load_B(object sender, EventArgs e)
{
B.maximun=10000000;
while(1==1)
{
for(i <0;i <10000000;i++)
{
if(i%100==0)
{
mypro.value=i;
}
}
}
}
------------------------------
form A button1_click(object sender, EventArgs e)
{ B b=new B();
b.show();//打开进度条窗体,问题出现了,这时进度条走的效果很差,跟本看不到渐增的效果。
fun()
{
………………;
}
}
---------------------------------
要求:给出代码,希望窗体B的进度条走的很清晰。(一格一格的走,不要看都看不见就满了,或fun()执行完了进度条才走。要边一边执行fun(),进度条一边一格一格地走。) 另外,我也试了用多线程异步调用(可能我的水平太差,不正确吧)。总之就是看不到窗体B进度条“mypro”一格一格走的样子。搞了一天,我要疯了。
我想给它加上进度条。
思路是这样的。在fun()函数前打开B窗体上面放一个进度条。(专门用于放进度条,没有边框的)。等执行完fun()后,关闭B窗体。
步骤:
新建了一个窗体B。上面放了一个进度条控件"mypro"。
在load_B事件中写下如代码(代码大小写有错,这个忽略,我主要是写出我的思路)
form B 的代码:
load_B(object sender, EventArgs e)
{
B.maximun=10000000;
while(1==1)
{
for(i <0;i <10000000;i++)
{
if(i%100==0)
{
mypro.value=i;
}
}
}
}
------------------------------
form A button1_click(object sender, EventArgs e)
{ B b=new B();
b.show();//打开进度条窗体,问题出现了,这时进度条走的效果很差,跟本看不到渐增的效果。
fun()
{
………………;
}
}
---------------------------------
要求:给出代码,希望窗体B的进度条走的很清晰。(一格一格的走,不要看都看不见就满了,或fun()执行完了进度条才走。要边一边执行fun(),进度条一边一格一格地走。) 另外,我也试了用多线程异步调用(可能我的水平太差,不正确吧)。总之就是看不到窗体B进度条“mypro”一格一格走的样子。搞了一天,我要疯了。
或用线程控制进度条。
fun() 建一个线程,放到后台去运行,
函数中修改全局变量记录当前执行的进度
显示进度的窗体上加个timer,通过访问全局变量刷新进度
判断进程状态,如果进程结束退出窗体
If th.ThreadState = Threading.ThreadState.Stopped Then
Me.Close()
End If
参考
Dim th As New System.Threading.Thread(AddressOf fun)
th.IsBackground = True
Dim frm As New FrmProperty
frm.f = Me
frm.th = th
th.Start()
frm.Show()
'启动timer
Private Sub FrmProperty_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
'timer中代码
Me.ProgressBar1.Value = p'进度的全局变量
If th.ThreadState = Threading.ThreadState.Stopped Then
Me.Close()
End If
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim btn As New Button With {.Text = "Test"}
AddHandler btn.Click, AddressOf fun
Me.Controls.Add(btn)
End Sub Private Sub fun(ByVal sender As Object, ByVal e As EventArgs)
Using frm As New frmProgressbar With {.Visible = True}
For i As Integer = 0 To 100
Me.Text = String.Format("{0}ms", i * 100)
Application.DoEvents()
Threading.Thread.Sleep(100)
Next
End Using
End Sub
End ClassPublic Class frmProgressbar
Inherits System.Windows.Forms.Form Private probar As ProgressBar
Private tmr As Timer Private Sub frmProgressbar_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
probar = New ProgressBar
Me.Controls.Add(probar)
tmr = New Timer With {.Interval = 200}
AddHandler tmr.Tick, AddressOf tick
tmr.Start()
End Sub Private Sub tick(ByVal sender As Object, ByVal e As EventArgs)
If probar.Value < probar.Maximum Then
probar.Value += 1
Else
probar.Value = probar.Minimum
End If
Me.Text = probar.Value.ToString
End SubEnd Class
http://www.cnblogs.com/wf5360308/articles/1343716.html
private event DoReportProgress onReportProgress; public void DisposeRecord()
{ for (int i=0; i<1000; i++)
{
// 工作 onReportProgress( (100 * i) / 1000 ); //通知进程条更改
}
} //同步更新进程条
private void OnReportProgress(int current)
{
if (this.InvokeRequired)
{
this.Invoke(new DoReportProgress(OnReportProgress), new object[] { current });
}
else
{
this.tsprogressMain.Maximum = 100;
this.tsprogressMain.Value = (int)current;
}
}
//在子窗体的Load事件中加载线程
private void MainForm_Load(object sender, EventArgs e)
{
this.onReportProgress = new DoReportProgress(OnReportProgress);
Thread tdDistill = new Thread(new ThreadStart(DisposeRecord)); //使用tdDistill线程用于后台数据处理
tdDistill.Start();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Button btn = new Button { Text = "Test" };
btn.Click += fun;
this.Controls.Add(btn);
} private void fun(object sender, EventArgs e)
{
using (frmProgressbar frm = new frmProgressbar { Visible = true })
{
for (int i = 0; i <= 100; i++)
{
this.Text = string.Format("{0}ms", i * 100);
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
}
} } public class frmProgressbar : System.Windows.Forms.Form
{ private ProgressBar probar;
private Timer tmr; public frmProgressbar ()
{
probar = new ProgressBar();
this.Controls.Add(probar);
tmr = new Timer { Interval = 200 };
tmr.Tick += tick;
tmr.Start();
} private void tick(object sender, EventArgs e)
{
if (probar.Value < probar.Maximum) {
probar.Value += 1;
}
else {
probar.Value = probar.Minimum;
}
this.Text = probar.Value.ToString();
} } }
private void fun(object sender, EventArgs e)
{
using (frmProgressbar frm = new frmProgressbar { Visible = true })
{
for (int i = 0; i <= 100; i++)
{
this.Text = string.Format("{0}ms", i * 100);
Application.DoEvents();
System.Threading.Thread.Sleep(100);
} }
}
主要是这句代码起了作用,Application.DoEvents();
但如果fun()不是一个循环环体呢?
那该如何做?
第二,B窗口进度条需要使用Sleep()将资源空一些出来用于处理逻辑
第三,B窗口需要使用Application,DoEvents(),否则看不到效果
不知道这样行不?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{ frmProgressbar frm=null; [BrowsableAttribute(false)]
public static bool CheckForIllegalCrossThreadCalls { get; set; }
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls=false ;
} private void Form1_Load(object sender, EventArgs e)
{
Button btn = new Button { Text = "Test" };
btn.Click += fun;
this.Controls.Add(btn);
} private void fun(object sender, EventArgs e)
{
frm = new frmProgressbar { Visible = true };
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(fun));
th.Start();
} private void fun()
{
System.Threading.Thread.Sleep(10000);
if (frm!=null) frm.Close();
} } public class frmProgressbar : System.Windows.Forms.Form
{ private ProgressBar probar;
private Timer tmr; public frmProgressbar()
{
probar = new ProgressBar();
this.Controls.Add(probar);
tmr = new Timer { Interval = 200 };
tmr.Tick += tick;
tmr.Start();
} private void tick(object sender, EventArgs e)
{
if (probar.Value < probar.Maximum)
{
probar.Value += 1;
}
else
{
probar.Value = probar.Minimum;
}
this.Text = probar.Value.ToString();
} }}
BackgroundWorker类包含3个事件,在事件处理程序中可进行异步操作辅助代码编写和同用户界面信息交互。
publiceventDoWorkEventHandler DoWork;//异步处理代码
publiceventProgressChangedEventHandler ProgressChanged;//处理进度汇报,同UI交互
publiceventRunWorkerCompletedEventHandler RunWorkerCompleted;//结束处理通知
代码太多不在贴出来,这里包含了完整的代码和分析,楼主可以参考后应该不会再有疑问,
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699305.aspx
1:以下代码是完整的,虽然有点长,是很全面进度条工具类。
2: 特点是调用简单,几分进度条各种应用情况都考虑到了,肯定可以找到你要用的情况。
3: 下面是一般调用的方,很简单,有多种进度条情况,根据你的需要调用。
private void btn_TestProgress_Click(object sender, EventArgs e){
this.btn_TestProgress.Enabled = false; //内部忙状态|Flash
try{
frm_Public_Process.ExcuteByProgress(
ProgressModel.BusyingIn2,
delegate(){
SqlConnection con1 = new SqlConnection(
"Data Source=(10.0.0.113);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Connection Timeout=5;"
);
frm_Public_Process.SetTip("Testing.New");
con1.Open();
}
);
}
catch (Exception exception){
MessageBox.Show("excepton1=" + exception.Message, "Message1");
} //内部忙状态|进度条
try{
frm_Public_Process.ExcuteByProgress(
ProgressModel.BusyingIn1,
delegate(){
SqlConnection con1 = new SqlConnection(
"Data Source=(10.0.0.114);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Connection Timeout=5;"
);
frm_Public_Process.SetTip("Testing.Progress");
con1.Open();
}
);
}
catch (Exception exception)
{
MessageBox.Show("excepton1=" + exception.Message, "Message2");
}
//无实际进度&非代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1);
frm_Public_Process.ShowCancel(false);
IAsyncResult result1 = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing1");
for (int i = 1; i <= 20; i++)
{
frm_Public_Process.SetProgress(i, 20);
Thread.Sleep(100);
}
}
)
);
while (!result1.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress(); //Ex
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut2);
frm_Public_Process.ShowCancel(false);
IAsyncResult result1Ex = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing1.Flash");
for (int i = 1; i <= 20; i++)
{
frm_Public_Process.SetProgress(i, 20);
Thread.Sleep(100);
}
}
)
);
while (!result1Ex.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress(); //无实际进度&非代理调用&有取消[此时完全表现为非模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1);
frm_Public_Process.ShowCancel(true);
IAsyncResult result2 = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing2");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 30);
Thread.Sleep(100);
}
}
)
);
while (!result2.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress(); //有实际进度&非代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.Progressing);
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing3");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 40);
Thread.Sleep(100);
}
frm_Public_Process.CancelProgress();...待续
//有实际进度&非代理调用&有取消[此时完全表现为非模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.Progressing);
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing4");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 50);
Thread.Sleep(100);
}
frm_Public_Process.CancelProgress(); //代理调用
try{
//有实际进度&代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.Progressing,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing5");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 100);
Thread.Sleep(100);
}
}
); //有实际进度&代理调用&有取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.Progressing,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing6");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 100);
if (i == 80){
frm_Public_Process.CancelProgress();
break;
}
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
}
}
); //无实际进度&代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut1,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing7");
for (int i = 1; i <= 20; i++)
{
Thread.Sleep(100);
Application.DoEvents();
}
}
); //Ex
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut2,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing7.Flash");
for (int i = 1; i <= 20; i++){
Thread.Sleep(100);
Application.DoEvents();
}
}
); //无实际进度&代理调用&有取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut1,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing8");
for (int i = 1; i <= 20; i++)
{
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
Application.DoEvents();
}
}
); //Ex
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut2,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing8.Flash");
for (int i = 1; i <= 20; i++){
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
Application.DoEvents();
}
}
);
}
finally{
this.btn_TestProgress.Enabled = true;
}
}
...待续
4:代码文件 frm_Public_Process.cs
using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;public delegate void CallbackMethod();//代理方
public delegate void CallbackMethodByParams(params object[] args);//代理方法[带可变参数]namespace Sysping.Presentation{
public enum ProgressModel{//进度条模式
BusyingOut1 = 0,//仅显示忙状态[忙部分的执行及刷新在外部|以进度条控件显示进度]
BusyingOut2 = 1,//仅显示忙状态[忙部分的执行及刷新在外部|以Flash动画显示进度]
BusyingIn1 = 2,//仅显示忙状态[忙部分的执行在内部|以进度条控件显示忙状态]
BusyingIn2 = 3,//仅显示忙状态[忙部分的执行在内部且以Flash动画显示忙状态]
Progressing = 4//有实际进度[调用方刷新]
} public partial class frm_Public_Process : Form {//进度窗体
#region 变量
private static bool JustEnableTimeSpan = true;//是否要计时
private DateTime JustStartTime = DateTime.Now;//时间
public static Exception JustException = null;//异常信息
private static string JustTip = "Busying";// 进度提示
private static CallbackMethod JustCallbackMethod = null;//来自调用方的代理方法
private static frm_Public_Process JustFrmProcess = null;//当前进度窗体
private static ProgressModel JustProgressModel = ProgressModel.BusyingOut1;//应用模式标志[忙状态|有进度]
private static bool IsBusyingIn //是忙状态还是无实际进度的模式{
get{return (frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn1) ||
(frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn2);
}
}
private static bool IsFlashMode{//是Flash动画方式的忙状态还是进度条方式的忙状态模式
get{
return (frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn2) ||
(frm_Public_Process.JustProgressModel == ProgressModel.BusyingOut2);
}
}
#endregion
...待续
#region 初始化
public frm_Public_Process(){
this.InitializeComponent();
frm_Public_Process.JustException = null;
}
private static frm_Public_Process CreateSingleInstance(){//创建单实例对象
if (frm_Public_Process.JustFrmProcess == null){
frm_Public_Process.JustFrmProcess = new frm_Public_Process();
}
return frm_Public_Process.JustFrmProcess;
}
#endregion
#region 方法.应用
public static void ExcuteByProgress(ProgressModel model, CallbackMethod callbackMethod, bool enableTimeSpan){
frm_Public_Process frmProcess = frm_Public_Process.CreateSingleInstance();
frm_Public_Process.JustEnableTimeSpan = enableTimeSpan;
frm_Public_Process.JustCallbackMethod = callbackMethod;
frm_Public_Process.JustProgressModel = model;
if (callbackMethod != null){
try{
frmProcess.ShowDialog();
frmProcess.Dispose();
frmProcess = null;
}
finally{
frm_Public_Process.JustFrmProcess = null;
}
if ((frm_Public_Process.IsBusyingIn) && (frm_Public_Process.JustException != null)){
throw frm_Public_Process.JustException;
}
}
else{
frmProcess.Show();
}
}
public static void ExcuteByProgress(ProgressModel model, CallbackMethod callbackMethod){
frm_Public_Process.ExcuteByProgress(model, callbackMethod, true);
}
public static void ExcuteByProgress(CallbackMethod callbackMethod){
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1, callbackMethod);
}
public static void ExcuteByProgress(ProgressModel model){
frm_Public_Process.ExcuteByProgress(model, null);
}
public static void SetProgress(int current, int total){//更新进度条
try{
if (frm_Public_Process.JustFrmProcess != null){
if ((current <= 0) || (total <= 0) || (total < current)) return;
frm_Public_Process.JustFrmProcess.Update();
if (frm_Public_Process.IsFlashMode){
frm_Public_Process.JustFrmProcess.pcb_This.Update();
}
else{
frm_Public_Process.JustFrmProcess.prg_This.Minimum = 1;
frm_Public_Process.JustFrmProcess.prg_This.Maximum = total;
frm_Public_Process.JustFrmProcess.prg_This.Value = current;
frm_Public_Process.JustFrmProcess.prg_This.Update();
}
if (frm_Public_Process.JustFrmProcess.btn_Close.Visible) Application.DoEvents();
}
}
catch { }
}
public static void SetTip(string tip){//更新进度条提示信息
try{
frm_Public_Process.JustTip = tip;
if (!frm_Public_Process.IsBusyingIn){
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.lab_Tip.Text = tip;
frm_Public_Process.JustFrmProcess.Update();
if (frm_Public_Process.JustFrmProcess.btn_Close.Visible) Application.DoEvents();
}
}
}
catch { }
}
public static void SetTimeSpanEnable(bool timeSpanEnable){//设置是否开启计时
try{
frm_Public_Process.JustEnableTimeSpan = timeSpanEnable;
if (!frm_Public_Process.IsBusyingIn){
if (frm_Public_Process.JustFrmProcess != null){
if (timeSpanEnable){
frm_Public_Process.JustFrmProcess.tim_This.Start();
}
else{
frm_Public_Process.JustFrmProcess.tim_This.Stop();
}
}
}
}
catch { }
}
public static void SetProgressModel(ProgressModel model){//设置进度条模式
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustProgressModel = model;
frm_Public_Process.JustFrmProcess.SetProgressBarStyle();
frm_Public_Process.JustFrmProcess.Update();
}
}
catch { }
}
public static void ShowCancel(bool show){//是否显示取消
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.Cursor = show ? Cursors.Default : Cursors.WaitCursor;
frm_Public_Process.JustFrmProcess.btn_Close.Visible = show;
frm_Public_Process.JustFrmProcess.Update();
}
}
catch { }
}
public static bool HasClickedCancel(){//是否用户点了取消控制
try{
if (frm_Public_Process.JustFrmProcess != null){
return frm_Public_Process.JustFrmProcess.DialogResult == DialogResult.Cancel;
}
}
catch { }
return false;
}
public static void CancelProgress(){//中途取消进度条
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.Release();
frm_Public_Process.JustFrmProcess.Dispose();
}
}
finally {
frm_Public_Process.JustFrmProcess = null;
}
}
#endregion
...待续
#region 事件
private void frm_Public_Process_Load(object sender, EventArgs e){
this.lab_Tip.Text = frm_Public_Process.JustTip;
this.SetProgressBarStyle();
if ((!frm_Public_Process.IsBusyingIn) && (frm_Public_Process.JustEnableTimeSpan)) this.tim_This.Start();
this.Cursor = this.btn_Close.Visible ? Cursors.Default : Cursors.WaitCursor;
this.lab_Tip.Update();
}
private void frm_Public_Process_Shown(object sender, EventArgs e){
this.Update();
if (frm_Public_Process.JustCallbackMethod != null){
try{
if (frm_Public_Process.IsBusyingIn){
IAsyncResult result = frm_Public_Process.JustCallbackMethod.BeginInvoke(null, null);
while (!result.IsCompleted){
this.lab_Tip.Text = frm_Public_Process.JustTip;
this.lab_Tip.Update();
if (frm_Public_Process.JustEnableTimeSpan) this.tim_This_Tick(null, null);
if (frm_Public_Process.IsFlashMode) this.pcb_This.Update();
else this.prg_This.Update();
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.JustCallbackMethod.EndInvoke(result);
}
else{
this.Invoke(frm_Public_Process.JustCallbackMethod);
if (this.DialogResult == DialogResult.None){
this.DialogResult = DialogResult.OK;
}
}
}
catch (Exception exception){
frm_Public_Process.JustException = exception;
}
finally{
this.Release();
}
}
}
private void SetProgressBarStyle(){
try{
this.pcb_This.Visible = frm_Public_Process.IsFlashMode;
this.prg_This.Visible = !this.pcb_This.Visible;
this.prg_This.Maximum = 100;
this.prg_This.Minimum = 1;
this.prg_This.Value = 10;
this.prg_This.Step = 1;
if (frm_Public_Process.JustProgressModel == ProgressModel.Progressing){
this.prg_This.Style = ProgressBarStyle.Blocks;
}
else{
this.prg_This.Style = ProgressBarStyle.Marquee;
}
}
catch
{ }
}
private void tim_This_Tick(object sender, EventArgs e){
TimeSpan span = DateTime.Now - this.JustStartTime;
this.lab_Span.Text = string.Format("{0:f}", span.TotalSeconds);
this.lab_Span.Visible = frm_Public_Process.JustEnableTimeSpan;
this.lab_Span.Update();
}
private void btn_Close_Click(object sender, EventArgs e){
this.Release();
}
private void Release(){
try{
this.tim_This.Stop();
this.tim_This.Dispose();
this.prg_This.Style = ProgressBarStyle.Blocks;
this.prg_This.Value = this.prg_This.Maximum;
this.Update();
Thread.Sleep(50);
}
finally{
if (this.DialogResult == DialogResult.None){
this.DialogResult = DialogResult.Cancel;
}
this.Cursor = Cursors.Default;
this.Close();
}
}
#endregion
}
}
...待续
//frm_Public_Process.designer.cs(画面代码)
namespace Sysping.Presentation{
partial class frm_Public_Process{
private System.ComponentModel.IContainer components = null;
public System.ComponentModel.ComponentResourceManager resources = null;
protected override void Dispose(bool disposing){
if (disposing && (components != null)){
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent(){
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm_Public_Process));
this.pan_Flash = new System.Windows.Forms.Panel();
this.pcb_This = new System.Windows.Forms.PictureBox();
this.lab_Tip = new System.Windows.Forms.Label();
this.lab_Span = new System.Windows.Forms.Label();
this.btn_Close = new DevExpress.XtraEditors.SimpleButton();
this.prg_This = new System.Windows.Forms.ProgressBar();
this.tim_This = new System.Windows.Forms.Timer(this.components);
this.pan_Flash.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pcb_This)).BeginInit();
this.SuspendLayout();
this.pan_Flash.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pan_Flash.Controls.Add(this.pcb_This);
this.pan_Flash.Controls.Add(this.lab_Tip);
this.pan_Flash.Controls.Add(this.lab_Span);
this.pan_Flash.Controls.Add(this.btn_Close);
this.pan_Flash.Controls.Add(this.prg_This);
this.pan_Flash.Dock = System.Windows.Forms.DockStyle.Fill;
this.pan_Flash.Location = new System.Drawing.Point(0, 0);
this.pan_Flash.Name = "pan_Flash";
this.pan_Flash.Size = new System.Drawing.Size(222, 79);
this.pan_Flash.TabIndex = 8;
this.pcb_This.Image = ((System.Drawing.Image)(resources.GetObject("pcb_This.Image")));
this.pcb_This.Location = new System.Drawing.Point(40, 25);
this.pcb_This.Name = "pcb_This";
this.pcb_This.Size = new System.Drawing.Size(149, 10);
this.pcb_This.TabIndex = 13;
this.pcb_This.TabStop = false;
this.pcb_This.Visible = false;
this.lab_Tip.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lab_Tip.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.lab_Tip.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lab_Tip.ForeColor = System.Drawing.Color.Maroon;
this.lab_Tip.Location = new System.Drawing.Point(0, 38);
this.lab_Tip.Name = "lab_Tip";
this.lab_Tip.Size = new System.Drawing.Size(220, 26);
this.lab_Tip.TabIndex = 9;
this.lab_Tip.Tag = "";
this.lab_Tip.Text = " Busying...";
this.lab_Tip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lab_Span.CausesValidation = false;
this.lab_Span.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lab_Span.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lab_Span.ForeColor = System.Drawing.Color.Blue;
this.lab_Span.ImageAlign = System.Drawing.ContentAlignment.BottomCenter;
this.lab_Span.Location = new System.Drawing.Point(0, 64);
this.lab_Span.Name = "lab_Span";
this.lab_Span.Size = new System.Drawing.Size(220, 13);
this.lab_Span.TabIndex = 12;
this.lab_Span.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lab_Span.UseWaitCursor = true;
this.btn_Close.Appearance.BackColor = System.Drawing.Color.Silver;
this.btn_Close.Appearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.btn_Close.Appearance.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_Close.Appearance.Options.UseBackColor = true;
this.btn_Close.Appearance.Options.UseBorderColor = true;
this.btn_Close.Appearance.Options.UseFont = true;
this.btn_Close.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.HotFlat;
this.btn_Close.Location = new System.Drawing.Point(204, 0);
this.btn_Close.Name = "btn_Close";
this.btn_Close.Size = new System.Drawing.Size(16, 16);
this.btn_Close.TabIndex = 10;
this.btn_Close.Text = "×";
this.btn_Close.Visible = false;
this.btn_Close.Click += new System.EventHandler(this.btn_Close_Click);
this.prg_This.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.prg_This.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.prg_This.ForeColor = System.Drawing.Color.Lime;
this.prg_This.Location = new System.Drawing.Point(39, 25);
this.prg_This.Name = "prg_This";
this.prg_This.Size = new System.Drawing.Size(150, 10);
this.prg_This.Step = 20;
this.prg_This.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
this.prg_This.TabIndex = 0;
this.prg_This.Value = 20;
this.tim_This.Tick += new System.EventHandler(this.tim_This_Tick);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.SkyBlue;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(222, 79);
this.Controls.Add(this.pan_Flash);
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frm_Public_Process";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Processing...";
this.TopMost = true;
this.Load += new System.EventHandler(this.frm_Public_Process_Load);
this.Shown += new System.EventHandler(this.frm_Public_Process_Shown);
this.pan_Flash.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pcb_This)).EndInit();
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.ProgressBar prg_This;
public System.Windows.Forms.Panel pan_Flash;
public System.Windows.Forms.Label lab_Tip;
private DevExpress.XtraEditors.SimpleButton btn_Close;
public System.Windows.Forms.Label lab_Span;
public System.Windows.Forms.Timer tim_This;
private System.Windows.Forms.PictureBox pcb_This;
}
}
2: 把以上代码组起来,就是一个完整通用的进度窗体工具类
3:调用简单,只有把想加进度的代码在外部如下套起来就可以了
frm_Public_Process.ExcuteByProgress
(
delegate(){
..你的代码...
}
);