C#使用线程实现进度条progressBar1
C#使用线程实现进度条progressBar1:使用环境:VS2013+Win7+32bit
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate void SetPos(int ipos);//代理
private void button1_Click(object sender, EventArgs e)
{
Thread fThread = new Thread(new ThreadStart(SleepT));
fThread.Start();
}
private void SleepT()
{
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(10);
SetTextMesssage(100 * i / 500);
}
}
private void SetTextMesssage(int ipos)
{
if (this.InvokeRequired)
{
SetPos setpos = new SetPos(SetTextMesssage);
this.Invoke(setpos, new object[] { ipos });
}
else
{
this.progressBar1.Value = Convert.ToInt32(ipos);
}
}
}
}
百度网盘:http://pan.baidu.com/s/1c2h3tW
// 进度条托管
private delegate void ShowProgressDelegate(int totalStep, int currentStep);
ProgreeLight ProgreeLightForm = new ProgreeLight();
private void ShowProgress(int currentStep, int totalStep)
{
if (ProgreeLightForm == null)
{
ProgreeLightForm = new ProgreeLight();
}
else
{
if (ProgreeLightForm.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示
{
ProgreeLightForm = new ProgreeLight();
}
}
ProgreeLightForm.Show();
ProgreeLightForm.SetProgreeBar(currentStep, totalStep);
}
调用:
ShowProgressDelegate showProgressBar = new ShowProgressDelegate(ShowProgress);
this.Invoke(showProgressBar, new object[] { 0, RowsCount_ });
页:
[1]