473,857 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Providing feedback during heavy calculation

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();
}
Nov 15 '05 #1
4 3280
Cant you register a callback? IF its lengthy, stick it in a thread and have
the caller register a callback or data or signal an event.

--

Jack Mayhoff
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Michiel Alsters" <dj************ **@hotmail.com> wrote in message
news:5c******** *************** ***@posting.goo gle.com...
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();
}

Nov 15 '05 #2
Michiel Alsters <dj************ **@hotmail.com> wrote:
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.


Rather than creating a timer to check the status, you should have your
calculation thread update the GUI periodically. You'll need to use
Control.Invoke to make sure the GUI is updated in the appropriate
thread.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
I'm not sure, but I think the problem might be that your "wait form" is not
created on the UI thread. I have a very similar application that works
well, but I designed it as follows:

A. Use an item on the UI form itself (vi the MDE Form designer), like a
progress bar, or text box, that will
display calculations/progress from the calculator thread.

B. Put a System.Windows. Forms.Timer on the UI form too (via the MDE Forms
designer's "toolbox"). I suppose a Threading.Timer would work as well,
but it is easier to let
the forms designer generate the timer's callback.

C. When the user Clicks the button to start the calculation:
1. Start the timer.
2. Start the long calculation's thread.

D. In the callback for the timer click (generated by the forms designer in
the MDE):
stop the timer.
lock the calculation thread's counter.
display the counter.
unlock the counter.
start the timer.

Be sure to lock the counter in the thread that does the long calculation
before you update it in that thread.

I believe the logic is about the same that you have, except the "progress
form" is just an item on the UI's form, the timer is a Forms.Timer, there is
no separate thread for updating the progress counter, but it is done on the
UI thread. Also, there is no need for the Application.DoE vents();

As others have suggested, it is sometimes better to let the long calculation
create an event to update UI. But your design has the advantage that the UI
is updated after a constant time interval, which is a bit more pleasing to
the eye.

"Michiel Alsters" <dj************ **@hotmail.com> wrote in message
news:5c******** *************** ***@posting.goo gle.com...
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();
}

Nov 15 '05 #4
Thanks, this helped a lot. I finally put both the calculation and the
message form in a seperate thread. This was the trick. This way I
could, as you sugested, suspend the calculation thread and update the
UI.

Thanks.

"Fred Mellender" <no************ ****@frontierne t.net> wrote in message news:<aO******* **********@news 02.roc.ny>...
I'm not sure, but I think the problem might be that your "wait form" is not
created on the UI thread. I have a very similar application that works
well, but I designed it as follows:

A. Use an item on the UI form itself (vi the MDE Form designer), like a
progress bar, or text box, that will
display calculations/progress from the calculator thread.

B. Put a System.Windows. Forms.Timer on the UI form too (via the MDE Forms
designer's "toolbox"). I suppose a Threading.Timer would work as well,
but it is easier to let
the forms designer generate the timer's callback.

C. When the user Clicks the button to start the calculation:
1. Start the timer.
2. Start the long calculation's thread.

D. In the callback for the timer click (generated by the forms designer in
the MDE):
stop the timer.
lock the calculation thread's counter.
display the counter.
unlock the counter.
start the timer.

Be sure to lock the counter in the thread that does the long calculation
before you update it in that thread.

I believe the logic is about the same that you have, except the "progress
form" is just an item on the UI's form, the timer is a Forms.Timer, there is
no separate thread for updating the progress counter, but it is done on the
UI thread. Also, there is no need for the Application.DoE vents();

As others have suggested, it is sometimes better to let the long calculation
create an event to update UI. But your design has the advantage that the UI
is updated after a constant time interval, which is a bit more pleasing to
the eye.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3781
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET actiavted Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
19
3066
by: Blair Adamache | last post by:
IBM is hosting a user focus session to get feedback on a new design concept for installing software products and maintenance. The information below gives a brief summary of the information about the session. If you are interested and feel that you are qualified, please send an e-mail to Karen Ball at kball@protocolusa.com. Or you can phone Sam Sellers or Flo Tassel at 800-615-9287. Protocol is a recruiting agency that IBM has hired to...
1
1849
by: Richard Hollenbeck | last post by:
I noticed I can't push a value into a text box by saying something like, "txtThisTextBox = intSomeVariable * 0.5" because I get an run-time error saying I can't assign a value to this object. So instead I made the control source a function to pull the value from the function. That works. Inside the text box I simply write something like =SomeFunction(). Beautiful. Works great.
38
2897
by: George Sexton | last post by:
We're in the process of re-launching our web calendar product. We would like to get your opinions on a couple of things about our site and product. This is a relaunch of an existing product with hundreds of customers. The major effort of the re-launch is to focus more on the hosted calendar market. Toward that end, we've re-designed our web site, and changed our pricing. Essentially, we've dropped the license fee that we were charging...
10
3217
by: roygon | last post by:
Hello, I have a C# application that runs a relatively complex simulation which, on a typical computer, could take up to 10 seconds. I am now trying to port this application over to ASP.NET so obviously this calculation needs to happen on the client. What is the best way to implement this? Some more information - all of the code needed to run this is located in 3 or 4 C# dll's so at a high level I would simply look at the user
1
1968
by: vincec | last post by:
Hi need some help from you guysin using visual C++. I am thinking of opening a text file after some calculation. For example, after i press the "calculation", then at the OnCalculation function, it will do some calcuation the it is written to a text file called "result.txt". Then i would like it to open the text file automatically after the calculation instead of i going to the folder to open it to view. Anyone know how to do so? Thanks
0
1975
by: k04jg02 | last post by:
Python has a nifty operator that will take a container and pass its elements as function parameters. In Python you can make a list like so: x = Then you can say: f(*x)
9
2506
by: gs | last post by:
the feedback for the install of c#2008 places 97 to 99% cpu load for way too long on athlon x64 3800+ PC. 3/4 an hour later its only about 80% complete, continuing with 98% CPU load! Next time installing visual studio /dot product I will likely make sure no feedback
5
1366
by: RobinS | last post by:
My company is considering moving up from .Net 2.0 to .Net 3.5(SP1) as a prerequisite on the ClickOnce deployment for our application. Does anybody have any experience deploying .Net 3.5 either as a prerequisite, or just by itself? Have you had any problems installing it? One issue I've seen reported is when pushed as a prerequisite, it progresses so far during installation, then sits there for a long time seemingly doing nothing, and...
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11082
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7939
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.