Home All Groups Group Topic Archive Search About

Do loop memory consumption?

Author
25 Apr 2006 9:37 PM
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

Author
25 Apr 2006 10:06 PM
Tom Shelton
Bill Nguyen wrote:
Show quoteHide quote
> 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]
Author
25 Apr 2006 10:47 PM
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


Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1146002783.330291.234260@j33g2000cwa.googlegroups.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]
>
Author
26 Apr 2006 1:07 AM
james
Insert an "Application.DoEvents: to allow for other proccesses to run. And
then in the btnStop's click event, stop the timer.
james

Show quoteHide quote
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
news:%23TKG6oLaGHA.3444@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" <t**@mtogden.com> wrote in message
> news:1146002783.330291.234260@j33g2000cwa.googlegroups.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]
>>
>
>
Author
26 Apr 2006 1:39 AM
ShaneO
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.
Author
26 Apr 2006 2:38 AM
Bill nguyen
This will definitely help!
My thanks to all of you in this thread.

Bill


Show quoteHide quote
"ShaneO" <shane***@optusnet.com.au> wrote in message
news:444ecf49$0$3341$afc38c87@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.
Author
26 Apr 2006 4:44 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
25 Apr 2006 10:38 PM
Mattias Sjögren
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.
Author
27 Apr 2006 4:56 AM
Bill nguyen
I'm willing to learn.
Can you please give me a sample code?
Thanks

Bill

Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:%23360uiLaGHA.4248@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.
Author
26 Apr 2006 2:47 AM
Spam Catcher
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in
news:uC21kBLaGHA.5088@TK2MSFTNGP03.phx.gbl:

> 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.
Author
26 Apr 2006 5:38 AM
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
Author
26 Apr 2006 4:50 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
26 Apr 2006 5:15 PM
Cor Ligthert [MVP]
>
>>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
Author
26 Apr 2006 5:00 PM
Spam Catcher
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in
news:uHinjNPaGHA.4916@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.
Author
26 Apr 2006 5:23 PM
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.

Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> schreef in bericht
news:Xns97B1843E472C7usenethoneypotrogers@127.0.0.1...
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in
> news:uHinjNPaGHA.4916@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.
Author
26 Apr 2006 10:19 PM
Spam Catcher
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in
news:#v51TXVaGHA.4160@TK2MSFTNGP04.phx.gbl:

> 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 : )
Author
26 Apr 2006 5:39 AM
Cor Ligthert [MVP]
> 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
Author
26 Apr 2006 8:11 AM
Göran_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.