// frmBaseClass.cs public delegate void ProgressStartEventHandle(object sender, int maxValue, int minValue); public delegate void ProgressChangeEventHandle(object sender,int currentValue,string tips); public delegate void ProgressEndEventHandle(object sender, string tips);
public partial class frmBaseClass : Form { public event ProgressStartEventHandle OnProgressStart; public event ProgressChangeEventHandle OnProgressChange; public event ProgressEndEventHandle OnProgressEnd; private frmProgressBar frmPB = null; public frmBaseClass() { InitializeComponent(); frmPB = new frmProgressBar(); OnProgressStart += new ProgressStartEventHandle(frmPB.ProgressStart); } protected virtual void TriggerProgressStart(object sender, int maxValue, int step) { if (OnProgressStart != null) { frmPB.Show(); // Show ProgressBar Form. OnProgressStart(sender,maxValue,step); } } protected virtual void TriggerProgressChange(object sender,int currentValue, string tips) { OnProgressChange += new ProgressChangeEventHandle(frmPB.ProgressChange); OnProgressChange(sender,currentValue,tips); } protected virtual void TriggerProgressEnd(object sender, string tips) { OnProgressEnd += new ProgressEndEventHandle(frmPB.ProgressEnd); OnProgressEnd(sender, tips); } } // frmProgressBar.cs public partial class frmProgressBar : Form {
public event ProgressStartEventHandle OnProgressStart;
public event ProgressChangeEventHandle OnProgressChange;
public event ProgressEndEventHandle OnProgressEnd;
private frmProgressBar frmPB = null;
public frmBaseClass()
{
InitializeComponent();
frmPB = new frmProgressBar();
OnProgressStart += new ProgressStartEventHandle(frmPB.ProgressStart);
}
protected virtual void TriggerProgressStart(object sender, int maxValue, int step)
if (OnProgressStart != null)
frmPB.Show(); // Show ProgressBar Form.
OnProgressStart(sender,maxValue,step);
protected virtual void TriggerProgressChange(object sender,int currentValue, string tips)
OnProgressChange += new ProgressChangeEventHandle(frmPB.ProgressChange);
OnProgressChange(sender,currentValue,tips);
protected virtual void TriggerProgressEnd(object sender, string tips)
OnProgressEnd += new ProgressEndEventHandle(frmPB.ProgressEnd);
OnProgressEnd(sender, tips);
System.Threading.ParameterizedThreadStart pts; System.Threading.Thread thread; public frmProgressBar() {
System.Threading.ParameterizedThreadStart pts;
System.Threading.Thread thread;
public frmProgressBar()
} public void ProgressStart(object sender, int maxValue, int minValue) {
public void ProgressStart(object sender, int maxValue, int minValue)
this.progressBar1.Maximum = maxValue; this.progressBar1.Minimum = minValue; this.progressBar1.Value = 1;
this.progressBar1.Maximum = maxValue;
this.progressBar1.Minimum = minValue;
this.progressBar1.Value = 1;
} public void ProgressChange(object sender, int currentValue, string tips) {
public void ProgressChange(object sender, int currentValue, string tips)
this.progressBar1.Value = currentValue; pts = new System.Threading.ParameterizedThreadStart(SetProgressTips); thread = new System.Threading.Thread(pts); thread.IsBackground = true; thread.Start((object)tips); // An error has occurred here.
this.progressBar1.Value = currentValue;
pts = new System.Threading.ParameterizedThreadStart(SetProgressTips);
thread = new System.Threading.Thread(pts);
thread.IsBackground = true;
thread.Start((object)tips); // An error has occurred here.
} public void ProgressEnd(object sender, string tips) {
public void ProgressEnd(object sender, string tips)
pts = new System.Threading.ParameterizedThreadStart(SetProgressTips); thread = new System.Threading.Thread(pts); thread.IsBackground = true; thread.Start((object)tips); System.Threading.Thread.Sleep(1000); this.Close(); // Close the ProgressBar Form.
thread.Start((object)tips);
System.Threading.Thread.Sleep(1000);
this.Close(); // Close the ProgressBar Form.
} private void SetProgressTips(object tips) {
private void SetProgressTips(object tips)
this.label1.Invoke((Action)(() => label1.Text = (string)tips));
public partial class Form1 : Test.frmBaseClass {
public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { TriggerProgressStart(this, 100, 1); for (int i = 1; i <=100; i++) { TriggerProgressChange(this,i,"Current Progress is:" + i.ToString()); System.Threading.Thread.Sleep(100); } TriggerProgressEnd(this, "Completed."); }
public Form1()
private void button2_Click(object sender, EventArgs e)
TriggerProgressStart(this, 100, 1);
for (int i = 1; i <=100; i++)
TriggerProgressChange(this,i,"Current Progress is:" + i.ToString());
System.Threading.Thread.Sleep(100);
TriggerProgressEnd(this, "Completed.");