Hello everybody,
I hope anybody can help me. I'll try to give a brief overview of my
problem. I have running a program that performs a heavy calculation.
To give the user feedback what the program is doing I show a window
which contains a progress bar and a label. At some point during the
execution the state of the calculation is changed, so I want to let
the user know this.
I have placed the creation of the form in a seperate thread and
created a timer that checks every second if the status is changed.
This timer works good, but the form is not refreshed, i.e. it is not
showing the new status.
What is the problem here?
I have included a piece of the code (see below).
Thanks for your time and help,
Michiel
public class CalcThread
{
private int counter;
private int calcStatus;
private System.Threadin g.Timer tmrCalc;
private frmWait frmCalc;
// Constructor accepts "parameters " meant for the ThreadStart
delegate.
public CalcThread()
{
counter = 0;
calcStatus = 0;
}
// ThreadStart delegate is a parameterless method of the threaded
class.
public void Launch()
{
/* Create a wait form */
frmCalc = new frmWait();
frmCalc.setStat eText(calcStatu s);
frmCalc.Show();
frmCalc.Refresh ();
/* Create the delegate that invokes methods for the timer */
TimerCallback timerDelegate = new TimerCallback(C heckStatus);
/* Create a timer that waits one second, then invokes every second
*/
System.Threadin g.Timer timer = new
System.Threadin g.Timer(timerDe legate, null, 1000, 1000);
/* Keep a handle to the timer, so it can be disposed */
tmrCalc = timer;
counter = 0;
}
public void Abort()
{
tmrCalc.Dispose ();
tmrCalc = null;
}
public void Update(int status)
{
calcStatus = status;
}
public int CalcTime()
{
return counter;
}
private void CheckStatus(Obj ect state)
{
counter++;
frmCalc.setStat eText(calcStatu s);
frmCalc.setCalc Text();
frmCalc.Refresh ();
Application.DoE vents();
Console.WriteLi ne("{0} Checking Status {1}.",
DateTime.Now.Ti meOfDay, counter);
}
}
/* The application */
private void btnCalculate_Cl ick(object sender, System.EventArg s e)
{
/* show calculation message */
calcStatus = 0;
CalcThread calcThread = new CalcThread();
thWait = new Thread(new ThreadStart(cal cThread.Launch) );
thWait.Start();
// The heavy calculation
calcThread.Upda te(++calcStatus );
// More heavy calculation
calc_time = calcThread.Calc Time();
calcThread.Abor t();
thWait.Abort();
}
I hope anybody can help me. I'll try to give a brief overview of my
problem. I have running a program that performs a heavy calculation.
To give the user feedback what the program is doing I show a window
which contains a progress bar and a label. At some point during the
execution the state of the calculation is changed, so I want to let
the user know this.
I have placed the creation of the form in a seperate thread and
created a timer that checks every second if the status is changed.
This timer works good, but the form is not refreshed, i.e. it is not
showing the new status.
What is the problem here?
I have included a piece of the code (see below).
Thanks for your time and help,
Michiel
public class CalcThread
{
private int counter;
private int calcStatus;
private System.Threadin g.Timer tmrCalc;
private frmWait frmCalc;
// Constructor accepts "parameters " meant for the ThreadStart
delegate.
public CalcThread()
{
counter = 0;
calcStatus = 0;
}
// ThreadStart delegate is a parameterless method of the threaded
class.
public void Launch()
{
/* Create a wait form */
frmCalc = new frmWait();
frmCalc.setStat eText(calcStatu s);
frmCalc.Show();
frmCalc.Refresh ();
/* Create the delegate that invokes methods for the timer */
TimerCallback timerDelegate = new TimerCallback(C heckStatus);
/* Create a timer that waits one second, then invokes every second
*/
System.Threadin g.Timer timer = new
System.Threadin g.Timer(timerDe legate, null, 1000, 1000);
/* Keep a handle to the timer, so it can be disposed */
tmrCalc = timer;
counter = 0;
}
public void Abort()
{
tmrCalc.Dispose ();
tmrCalc = null;
}
public void Update(int status)
{
calcStatus = status;
}
public int CalcTime()
{
return counter;
}
private void CheckStatus(Obj ect state)
{
counter++;
frmCalc.setStat eText(calcStatu s);
frmCalc.setCalc Text();
frmCalc.Refresh ();
Application.DoE vents();
Console.WriteLi ne("{0} Checking Status {1}.",
DateTime.Now.Ti meOfDay, counter);
}
}
/* The application */
private void btnCalculate_Cl ick(object sender, System.EventArg s e)
{
/* show calculation message */
calcStatus = 0;
CalcThread calcThread = new CalcThread();
thWait = new Thread(new ThreadStart(cal cThread.Launch) );
thWait.Start();
// The heavy calculation
calcThread.Upda te(++calcStatus );
// More heavy calculation
calc_time = calcThread.Calc Time();
calcThread.Abor t();
thWait.Abort();
}
Comment