110,536
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
using System;
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();
}
}
}
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();
}
}