| | |
Reducing CPU usage
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 6
Reputation:
Solved Threads: 0
I have written this code to Display the time every 15 minutes using MSAgent:
(Private Sub Form_Load()
On Error Resume Next
CenterForm
LoadAgent "merlin"
theAgent.Speak "Hello"
theAgent.Hide
Gettime:
Do
Mytime = Time
Myhour = Hour(Mytime)
Myminute = Minute(Mytime)
Mysecond = Second(Mytime)
If Myminute = 15 And Mysecond = 0 Then Exit Do
If Myminute = 30 And Mysecond = 0 Then Exit Do
If Myminute = 45 And Mysecond = 0 Then Exit Do
If Myminute = 0 And Mysecond = 0 Then Exit Do
Debug.Print Myminute, Mysecond
Loop
Debug.Print "Load"
theAgent.Show
theAgent.Play "announce"
theAgent.Speak "The time is " & Myhour & ":" & Myminute & "."
theAgent.Play "announcereturn"
theAgent.Hide
Do Until Mysecond > 0
Mysecond = Second(Time)
Loop
intCnt = 15000
start = Timer
waitTime = start + intCnt
Debug.Print Timer, waitTime, "start"
Do While Timer < waitTime
Debug.Print Timer, waitTime
DoEvents
Loop
GoTo Gettime
End Sub)
The Code works. So far but it uses 50%cpu. Are there any ways of writing the timer delay routine to reduce the cpu usage. Sleep doesnt seem to be available in VB5
I'm a novice at writing code been away from coding for several years.
The OS in win XP and VB is version 5 with sp3.
(Private Sub Form_Load()
On Error Resume Next
CenterForm
LoadAgent "merlin"
theAgent.Speak "Hello"
theAgent.Hide
Gettime:
Do
Mytime = Time
Myhour = Hour(Mytime)
Myminute = Minute(Mytime)
Mysecond = Second(Mytime)
If Myminute = 15 And Mysecond = 0 Then Exit Do
If Myminute = 30 And Mysecond = 0 Then Exit Do
If Myminute = 45 And Mysecond = 0 Then Exit Do
If Myminute = 0 And Mysecond = 0 Then Exit Do
Debug.Print Myminute, Mysecond
Loop
Debug.Print "Load"
theAgent.Show
theAgent.Play "announce"
theAgent.Speak "The time is " & Myhour & ":" & Myminute & "."
theAgent.Play "announcereturn"
theAgent.Hide
Do Until Mysecond > 0
Mysecond = Second(Time)
Loop
intCnt = 15000
start = Timer
waitTime = start + intCnt
Debug.Print Timer, waitTime, "start"
Do While Timer < waitTime
Debug.Print Timer, waitTime
DoEvents
Loop
GoTo Gettime
End Sub)
The Code works. So far but it uses 50%cpu. Are there any ways of writing the timer delay routine to reduce the cpu usage. Sleep doesnt seem to be available in VB5
I'm a novice at writing code been away from coding for several years.
The OS in win XP and VB is version 5 with sp3.
Use a timer control instead of a loop. This should all but eliminate your CUP Usage.
Just a a quick test, I created a new project, set form1 to open form2 every 2 seconds via timer control, and put a timer on form2 which unloaded from memory every 1 second.
I coun't get anymore than 15% CPU usage with avg around 7%. With the timer set to 150000 on form1, CPU usage was avg 2%, max 5% when counting down and max 7% when actually loading and unload form2.
Of course, CPU usage will vary depending on what system specs you have.
Mine is Athlon XP 3000+ (2.17 GHz) and 768MB RAM.
Just a a quick test, I created a new project, set form1 to open form2 every 2 seconds via timer control, and put a timer on form2 which unloaded from memory every 1 second.
I coun't get anymore than 15% CPU usage with avg around 7%. With the timer set to 150000 on form1, CPU usage was avg 2%, max 5% when counting down and max 7% when actually loading and unload form2.
Of course, CPU usage will vary depending on what system specs you have.
Mine is Athlon XP 3000+ (2.17 GHz) and 768MB RAM.
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
•
•
Join Date: Jun 2006
Posts: 6
Reputation:
Solved Threads: 0
Thanks Guys.
Ok i tried using a timer control but i cant get the timer to start. Maybe you can tell me whats wrong. Here's the code:
Ok i tried using a timer control but i cant get the timer to start. Maybe you can tell me whats wrong. Here's the code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub Form_Load() On Error Resume Next CenterForm LoadAgent "merlin" theAgent.Speak "Hello" theAgent.Hide Gettime: Do Mytime = Time Myhour = Hour(Mytime) Myminute = Minute(Mytime) Mysecond = Second(Mytime) Debug.Print Mysecond, Myminute If Mysecond = 0 Then If Myminute = 15 Or Myminute = 30 Or Myminute = 45 Or Myminute = 1 Then Exit Do End If Loop theAgent.Show theAgent.Play "announce" theAgent.Speak "The time is " & Myhour & ":" & Myminute & "." theAgent.Play "announcereturn" theAgent.Hide Do Until Mysecond > 0 Mysecond = Second(Time) Loop Timer1.Interval = 60000 Timer1.Enabled = True GoTo Gettime End Sub Private Sub Timer1_Timer() If intCnt = 14 Then intCnt = 1 Else intCnt = intCnt + 1 Debug.Print intCnt End If End Sub
Don't force it, Use a bigger hammer!
•
•
Join Date: Jun 2006
Posts: 6
Reputation:
Solved Threads: 0
Absolutely stumped as to why the timer subroutine isn't called in this code. I tested another program using the timer and it runs without a hitch.
Have even moved the interval into the setup box.
What would stop the timer routine from executing in this code?
Have even moved the interval into the setup box.
What would stop the timer routine from executing in this code?
Last edited by joe935; Jun 26th, 2006 at 9:07 am.
Don't force it, Use a bigger hammer!
I've rearrainged your code a little although you may still need to adjust a thing or two to make it work.
Basically, you don't need a continous loop in form_load, the timer takes care of that. Try This:
Basically, you don't need a continous loop in form_load, the timer takes care of that. Try This:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub Form_Load() On Error Resume Next CenterForm LoadAgent "merlin" theAgent.Speak "Hello" theAgent.Hide Timer1.Interval = 60000 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Mytime = Time Myhour = Hour(Mytime) Myminute = Minute(Mytime) Mysecond = Second(Mytime) Debug.Print Mysecond, Myminute If Mysecond = 0 Then If Myminute = 15 Or Myminute = 30 Or Myminute = 45 Or Myminute = 1 Then saytime("The time is " & Myhour & ":" & Myminute & ".") End If End Sub Private Sub sayTime(txt as string) theAgent.Show theAgent.Play "announce" theAgent.Speak txt theAgent.Play "announcereturn" theAgent.Hide End Sub
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
The Timer Control is a control that makes a certain set of code execute at a given interval. It gets set in milliseconds, so 1000 is 1 second in the interval. The purpose of a loop is to execute a given set a code either a given number of times, or until a specific condition forces a change that will stop the loop.... a timer control just simply executes a set of instructions every so often. Also, with a timer control, your app is free to have other events happen, in between (or even during) timer events.... which reduces your need to use doevents, and also saves on Processor Time.
•
•
Join Date: Jun 2006
Posts: 6
Reputation:
Solved Threads: 0
Thanks Comotose for the clarification.
And Thanks to Agrothe for the coding . Im Testing it now.
The thing that threw me for a loop was:
Since the timer was set to an interval of 60000 and in step mode it took a minute before the actual subroutine was being executed and since i didn't wait long enough it looked like it wasn't being called.
And Thanks to Agrothe for the coding . Im Testing it now.
The thing that threw me for a loop was:
Since the timer was set to an interval of 60000 and in step mode it took a minute before the actual subroutine was being executed and since i didn't wait long enough it looked like it wasn't being called.
Last edited by joe935; Jun 26th, 2006 at 11:13 am.
Don't force it, Use a bigger hammer!
•
•
•
•
Originally Posted by Comatose
The Timer Control is a control that makes a certain set of code execute at a given interval. It gets set in milliseconds, so 1000 is 1 second in the interval. The purpose of a loop is to execute a given set a code either a given number of times, or until a specific condition forces a change that will stop the loop.... a timer control just simply executes a set of instructions every so often. Also, with a timer control, your app is free to have other events happen, in between (or even during) timer events.... which reduces your need to use doevents, and also saves on Processor Time.
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
![]() |
Similar Threads
- svchost.exe 100% cpu usage pissing me off :( (Windows NT / 2000 / XP)
- 100% CPU USAGE on and off! ;/ (Windows NT / 2000 / XP)
- CPU Usage at 100% with Nero 6 (Windows NT / 2000 / XP)
- 100% cpu usage when transcoding movie in nero 6 (Windows NT / 2000 / XP)
- 100% CPU Usage - No Virus, No gaming (Windows NT / 2000 / XP)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Text to Speech
- Next Thread: Include datafield into the section 4 of the Datareport
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






