1.滾動條
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class MyProgressBar : Form
{
public MyProgressBar()
{
InitializeComponent();
Initial();
}
public static int maxValue = 0;
public static int minValue = 0;
public void Initial()
{
this.progressBar1.Maximum = 100;
this.progressBar1.Minimum = 0;
maxValue = 100;
minValue = 0;
}
//public delegate void UpdateValue(int value);
public bool UpdateValue(int nValue)
{
while (!this.IsHandleCreated)
{
//MessageBox.Show("等待窗體句柄創建");
};
//try
//{
this.Invoke(new Action(() =>
{
//if (this.Handle == null) return ;
if (nValue > 0)
{
this.label1.Text = nValue.ToString()+"%";
if ( nValue < progressBar1.Maximum)
{
progressBar1.Value =nValue;
}
else
{
progressBar1.Value = progressBar1.Maximum;
this.Close();
}
}
}));
return true;
//}
//catch(Exception ee)
//{
//MessageBox.Show(ee.ToString());
return true;
//}
}
}
}
2.主窗體調用
public delegate bool UpdateProgressValue(int value);
public event UpdateProgressValue UpdateProgressEvent;
public void CreatedataLineFile(string folderPath)
{
//創建進度條
Thread th = new Thread(() =>
{
MyProgressBar pb = new MyProgressBar();
pb.StartPosition = FormStartPosition.CenterScreen;
UpdateProgressEvent += pb.UpdateValue;
pb.ShowDialog();
});
th.Start();
while (UpdateProgressEvent == null) ;
for (int i = 0; i < 1000;i++ )
{
Thread.Sleep(10);
UpdateProgressEvent(Convert.ToInt32(i * 1.0 / 1000 * (MyProgressBar.maxValue - MyProgressBar.minValue)+MyProgressBar.minValue));
}
return;
}