Hello friend,
My question is as follows:
codes:
// Test.dll
namespace Test
{
// frmBaseClass.cspublic 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.cspublic partial class frmProgressBar : Form{
System.Threading.ParameterizedThreadStart pts;System.Threading.Thread thread;public frmProgressBar(){
InitializeComponent();
}public void ProgressStart(object sender, int maxValue, int minValue){
this.progressBar1.Maximum = maxValue;this.progressBar1.Minimum = minValue;this.progressBar1.Value = 1;
}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.
}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.
}private void SetProgressTips(object tips){
this.label1.Invoke((Action)(() => label1.Text = (string)tips));
}
}
}
// Demo(The project references Test.dll)
namespace Demo
{
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.");}
}
}
In addition,the "tips" are not displayed.
Thanks in advance.

VulpesPosted Feb 7, 2015, 5:54 PM
An easy way to solve this is to use Application.DoEvents to force all current messages in the message queue to be processed.
You also need to set the ProgressBar value to 100 in the ProgressEnd method.
Even though the tips are now showing if (like me) you're using Windows 7 (and possibly 8), then you'll find that the ProgressBar never actually reaches 100 due to a problem with the Aero interface. This can be solved using a hack - you set it to 100, then 99 and finally back to 100! Don't ask me why but it works :)
If after running the ProgressBar once, you press the button to start it again, you'll get the 'object disposed' error because the original instance of frmProgressBar has been closed and hence disposed of. An easy way to deal with this is not to close it but to make it invisible instead.
So, I've highlighted the changes in the code you'll need to make to deal with these points:
/* frmProgressBar */
Ken HPosted Feb 7, 2015, 9:14 PM
Ken HPosted Feb 6, 2015, 11:34 PM
VulpesPosted Feb 6, 2015, 4:35 PM
Can you zip/rar and upload it, please.
Ken HPosted Feb 4, 2015, 8:10 PM
1)Unable to access the object has been released;
2)Form1 appears unable to respond;
3)The label1 of frmProgressBar not show tips.
How to solve them?
VulpesPosted Feb 4, 2015, 10:16 AM