现在有一个封装好了的数据处理函数 DataExcu() 处理数据需要5分钟左右
因此想弄个进度条 如下:
1.Form1中设置DataExcu()函数的参数后 点击按钮“开始处理” 然后弹出一个新的窗体Form2
Form2带进度条 当Form1数据处理完成时候 From2显示完成 进度条隐藏 (小弟看了多线程的例子 晕乎乎的)
本人小菜 谢谢各位了先
因此想弄个进度条 如下:
1.Form1中设置DataExcu()函数的参数后 点击按钮“开始处理” 然后弹出一个新的窗体Form2
Form2带进度条 当Form1数据处理完成时候 From2显示完成 进度条隐藏 (小弟看了多线程的例子 晕乎乎的)
本人小菜 谢谢各位了先
如果不能的话,启动一个Timer(Forms.Timer)来模拟进度。由于跳表事件发生在UI线程,所以可以直接在timer_Tick中更新进度条。启动Timer的同时开始运行DataExec,运行完毕后通知UI线程停止Timer和进度条。ThreadPool.QueueUserWorkItem(delegate
{
this.DataExec();
this.OnDataExecDone(this, EventArg.Empty);
});void OnDataExecDone(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(OnDataExecDone));
}
else
{
HideProgressBar();
}
}
点击Form1中的按钮弹出Form2,在Form2中启动处理事件,同时在Form2中显示进度,等Form2中处理完之后关闭Form2。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication192
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); Button B = new Button();
B.Parent = this;
B.Click += new EventHandler(B_Click);
} void B_Click(object sender, EventArgs e)
{
DataExcu();
} void DataExcu()
{
ProgressBar PB = new ProgressBar();
PB.Parent = this;
PB.BringToFront();
PB.Maximum = 20000;
PB.Location = new Point((this.ClientRectangle.Width - PB.Width) / 2,
(this.ClientRectangle.Height - PB.Height) / 2); new Thread(new ParameterizedThreadStart(DoStep)).Start(PB); } void DoStep(Object Obj)
{
ProgressBar PB = (ProgressBar)Obj; for (int i = 0; i < PB.Maximum; i++)
this.Invoke(new SetProgressBarValue(DoSetProgressBarValue), new Object[] { PB });
} delegate void SetProgressBarValue(ProgressBar PB); void DoSetProgressBarValue(ProgressBar PB)
{
if (++PB.Value == PB.Maximum)
PB.Dispose();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ProgressForm progressForm = new ProgressForm();
ThreadPool.QueueUserWorkItem(delegate
{
DataExec();
progressForm.End(); //<--关闭进度条
});
progressForm.ShowDialog(this);
}
}class ProgressForm : Form
{
int progress = 0, target = 5 * 60;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); public ProgressForm()
{
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(200, 32);
this.timer.Tick += delegate { progress++; this.Invalidate(); };
this.timer.Start();
} public void End()
{
this.Invoke(new MethodInvoker(this.Close()));
} protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.PeachPuff);
int width = this.Width * progress / target;
width = Math.Min(width, this.Width);
e.Graphics.FillRectangle(Brushes.Aqua, 0, 0, width, this.Height);
}
}
bkgWorker.WorkerReportsProgress = true; private void bkgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
进度条psBarAll.Value = e.ProgressPercentage+1;
}
{
做具体的事情
}
{
new System.Threading.Thread(new System.Threading.ThreadStart(Start)).Start();
}
public void Start()
{
}
void A(long total, long current)
{
if (this.InvokeRequired)
{
//调用方法
}
else
{
this.progressBar1.Maximum = (int)total;
this.progressBar1.Value = (int)current;
}
}
数据处理函数 DataExcu() 是有参数的。