By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Do loop memory consumption?

P: n/a
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
Apr 25 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

Bill Nguyen wrote:
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 ().

--
Tom Shelton [MVP]

Apr 25 '06 #2

P: n/a
Bill,

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 25 '06 #3

P: n/a
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
"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

Bill Nguyen wrote:
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 ().

--
Tom Shelton [MVP]

Apr 25 '06 #4

P: n/a
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" <bi*****************@jaco.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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
"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

Bill Nguyen wrote:
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 ().

--
Tom Shelton [MVP]


Apr 26 '06 #5

P: n/a
Bill Nguyen wrote:
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.
Apr 26 '06 #6

P: n/a
This will definitely help!
My thanks to all of you in this thread.

Bill
"ShaneO" <sh******@optusnet.com.au> wrote in message
news:44**********************@news.optusnet.com.au ...
Bill Nguyen wrote:
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.

Apr 26 '06 #7

P: n/a
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
Apr 26 '06 #8

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


Why? see for the rest my reply to Mattias.

Cor
Apr 26 '06 #9

P: n/a
> 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.
Apr 26 '06 #10

P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 26 '06 #11

P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 26 '06 #12

P: n/a
>
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.

Forget it, I just misreaded your messages, we agree completely.

Sorry

:-)

Cor
Apr 26 '06 #13

P: n/a
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.

"Spam Catcher" <sp**********@rogers.com> schreef in bericht
news:Xn**********************************@127.0.0. 1...
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in
news:uH**************@TK2MSFTNGP04.phx.gbl:
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.

Apr 26 '06 #14

P: n/a
I'm willing to learn.
Can you please give me a sample code?
Thanks

Bill

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Bill,

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 27 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.