6
Answers

out of memory exception in C#?

Ken H

Ken H

9y
1.3k
1
Hello friend,
     My question is as follows:
 
codes:
 
// Test.dll 
namespace Test
{
//  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
{
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. 
 
Answers (6)