Do loop memory consumption?

B

Bill Nguyen

The Timer loop below consumes all available CPU time from my development PC.
I would like to be able to restrict its use of CPU time to minimum since
it's not actually doing anything but counting down the time left.
Any help is greatly appreciated.

Bill


Dim t As Integer = 0

Dim Start, Finish, TotalTime As Double

'Start = Microsoft.VisualBasic.DateAndTime.Timer

Finish = Start + mSeconds ' Set end time for mseconds duration.

Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish

t += 1

lblInfo.Text = "test timer -> " & t

lblInfo.Refresh()

' Do other processing while waiting for 5mseconds to elapse.

Loop
 
T

Tom Shelton

Bill said:
The Timer loop below consumes all available CPU time from my development PC.
I would like to be able to restrict its use of CPU time to minimum since
it's not actually doing anything but counting down the time left.
Any help is greatly appreciated.

Bill


Dim t As Integer = 0

Dim Start, Finish, TotalTime As Double

'Start = Microsoft.VisualBasic.DateAndTime.Timer

Finish = Start + mSeconds ' Set end time for mseconds duration.

Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish

t += 1

lblInfo.Text = "test timer -> " & t

lblInfo.Refresh()

' Do other processing while waiting for 5mseconds to elapse.

Loop

One way to fix this would be to insert System.Thread.Sleep (100) right
after your lblInfo.Refresh ().
 
M

Mattias Sjögren

Bill,

Have you considered using System.Windows.Forms.Timer instead?


Mattias
 
B

Bill Nguyen

Tom;

That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
went down significantly. memory used still about the same, but that I can
afford!
Another question, please:
I need to interrupt the loop when user presses the STOP button. The problem
that btnStop(on the same form) is not accessible during the loop.
Is there a way to check if the button was pressed during the do loop
routine?

Thanks again;

Bill
 
J

james

Insert an "Application.DoEvents: to allow for other proccesses to run. And
then in the btnStop's click event, stop the timer.
james

Bill Nguyen said:
Tom;

That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
went down significantly. memory used still about the same, but that I can
afford!
Another question, please:
I need to interrupt the loop when user presses the STOP button. The
problem that btnStop(on the same form) is not accessible during the loop.
Is there a way to check if the button was pressed during the do loop
routine?

Thanks again;

Bill
 
S

ShaneO

Bill said:
Tom;

That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
went down significantly. memory used still about the same, but that I can
afford!
Another question, please:
I need to interrupt the loop when user presses the STOP button. The problem
that btnStop(on the same form) is not accessible during the loop.
Is there a way to check if the button was pressed during the do loop
routine?

Hello Bill,

I'd consider changing you code as follows (Watch for line-wrapping) -

Dim blExitLoop as Boolean = False 'Place this in General Declarations
Section

Dim t As Integer = 0
Dim Start, Finish, TotalTime As Double
'Start = Microsoft.VisualBasic.DateAndTime.Timer
Finish = Start + mSeconds ' Set end time for mseconds duration.
Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish And blExitLoop
= False
t += 1
lblInfo.Text = "test timer -> " & t
'lblInfo.Refresh() 'You can REMOVE this now!
System.Threading.Thread.Sleep(100)
System.Windows.Forms.Application.DoEvents()

' Do other processing while waiting for 5mseconds to elapse.
Loop

(Note the change to your "Do While... Loop above!)

I'd then put the following in your "Stop" Button Click Event -

blExitLoop = True


I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
G

Guest

The Timer loop below consumes all available CPU time from my
development PC. I would like to be able to restrict its use of CPU
time to minimum since it's not actually doing anything but counting
down the time left. Any help is greatly appreciated.

For stuff like this... you should be using a Windows Timer to process
repeatitve GUI events.
 
C

Cor Ligthert [MVP]

For stuff like this... you should be using a Windows Timer to process
repeatitve GUI events.

Serious why, I have the idea that once somebody wrote this on a blog and no
everybody is parroting it. In my opinion is for UI (forms) application the
forms timer excellent.

But I can be wrong.

Cor

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The Timer loop below consumes all available CPU time from my development PC.
I would like to be able to restrict its use of CPU time to minimum since
it's not actually doing anything but counting down the time left.

Use Thread.Sleep to make a delay without using CPU time.
 
M

Mattias Sjögren

I need to interrupt the loop when user presses the STOP button. The problem
that btnStop(on the same form) is not accessible during the loop.

That's exactly why you shouldn't sleep on a UI thread. And DoEvents
has its own problems. Again, try it with a timer.


Mattias
 
M

Mattias Sjögren

In my opinion is for UI (forms) application the
forms timer excellent.

I was under the impression that Bill's code is in a winforms
application. So I'm not sure if you're disagreeing with us or what
your point is.


Mattias
 
G

Guest

Serious why, I have the idea that once somebody wrote this on a blog
and no everybody is parroting it. In my opinion is for UI (forms)
application the forms timer excellent.

There are 3 timers in .NET - each one for a specific use.

So Timers could be used in forms, in server code, or as a general timer.
Depends on which class and which scenario.

Better than having a constant loop.

Of course it depends on the situation - some things are better suited for a
thread looping and others for a timer.
 
C

Cor Ligthert [MVP]

I was under the impression that Bill's code is in a winforms
application. So I'm not sure if you're disagreeing with us or what
your point is.
Forget it, I just misreaded your messages, we agree completely.

Sorry

:)

Cor
 
C

Cor Ligthert [MVP]

Spam Catcher,

The same as to Mattias,

I misreaded your messages I thougt you were pointing to the
system.systems.timer as I had seen done more, while the forms timer is in my
idea sufficient in this case.

So I thought maybe they have a reason for that and than I want to know why.

Sorry,

Cor.
 
G

Guest

Spam Catcher,

The same as to Mattias,

I misreaded your messages I thougt you were pointing to the
system.systems.timer as I had seen done more, while the forms timer is
in my idea sufficient in this case.

NP, I read your other posts in other groups and they're usually pretty
accurate so I knew it was probably a misunderstanding : )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top