Reducing CPU usage

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 6
Reputation: joe935 is an unknown quantity at this point 
Solved Threads: 0
joe935 joe935 is offline Offline
Newbie Poster

Reducing CPU usage

 
0
  #1
Jun 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Re: Reducing CPU usage

 
0
  #2
Jun 25th, 2006
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.
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Reducing CPU usage

 
0
  #3
Jun 26th, 2006
Yup, he's right. Truth is, that's exactly what the timer control is for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 6
Reputation: joe935 is an unknown quantity at this point 
Solved Threads: 0
joe935 joe935 is offline Offline
Newbie Poster

Re: Reducing CPU usage

 
0
  #4
Jun 26th, 2006
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. On Error Resume Next
  3. CenterForm
  4. LoadAgent "merlin"
  5. theAgent.Speak "Hello"
  6. theAgent.Hide
  7. Gettime:
  8. Do
  9. Mytime = Time
  10. Myhour = Hour(Mytime)
  11. Myminute = Minute(Mytime)
  12. Mysecond = Second(Mytime)
  13. Debug.Print Mysecond, Myminute
  14. If Mysecond = 0 Then
  15. If Myminute = 15 Or Myminute = 30 Or Myminute = 45 Or Myminute = 1 Then Exit Do
  16. End If
  17. Loop
  18. theAgent.Show
  19. theAgent.Play "announce"
  20. theAgent.Speak "The time is " & Myhour & ":" & Myminute & "."
  21. theAgent.Play "announcereturn"
  22. theAgent.Hide
  23. Do Until Mysecond > 0
  24. Mysecond = Second(Time)
  25. Loop
  26.  
  27. Timer1.Interval = 60000
  28. Timer1.Enabled = True
  29. GoTo Gettime
  30.  
  31.  
  32. End Sub
  33.  
  34. Private Sub Timer1_Timer()
  35. If intCnt = 14 Then
  36. intCnt = 1
  37. Else
  38. intCnt = intCnt + 1
  39. Debug.Print intCnt
  40. End If
  41. End Sub
Don't force it, Use a bigger hammer!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 6
Reputation: joe935 is an unknown quantity at this point 
Solved Threads: 0
joe935 joe935 is offline Offline
Newbie Poster

Re: Reducing CPU usage

 
0
  #5
Jun 26th, 2006
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?
Last edited by joe935; Jun 26th, 2006 at 9:07 am.
Don't force it, Use a bigger hammer!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Re: Reducing CPU usage

 
0
  #6
Jun 26th, 2006
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. On Error Resume Next
  3. CenterForm
  4. LoadAgent "merlin"
  5. theAgent.Speak "Hello"
  6. theAgent.Hide
  7.  
  8. Timer1.Interval = 60000
  9. Timer1.Enabled = True
  10.  
  11. End Sub
  12.  
  13. Private Sub Timer1_Timer()
  14. Mytime = Time
  15. Myhour = Hour(Mytime)
  16. Myminute = Minute(Mytime)
  17. Mysecond = Second(Mytime)
  18. Debug.Print Mysecond, Myminute
  19. If Mysecond = 0 Then
  20. If Myminute = 15 Or Myminute = 30 Or Myminute = 45 Or Myminute = 1 Then saytime("The time is " & Myhour & ":" & Myminute & ".")
  21. End If
  22. End Sub
  23.  
  24. Private Sub sayTime(txt as string)
  25. theAgent.Show
  26. theAgent.Play "announce"
  27. theAgent.Speak txt
  28. theAgent.Play "announcereturn"
  29. theAgent.Hide
  30. End Sub
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Reducing CPU usage

 
0
  #7
Jun 26th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 6
Reputation: joe935 is an unknown quantity at this point 
Solved Threads: 0
joe935 joe935 is offline Offline
Newbie Poster

Re: Reducing CPU usage

 
0
  #8
Jun 26th, 2006
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.
Last edited by joe935; Jun 26th, 2006 at 11:13 am.
Don't force it, Use a bigger hammer!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 119
Reputation: agrothe is an unknown quantity at this point 
Solved Threads: 14
agrothe's Avatar
agrothe agrothe is offline Offline
Junior Poster

Re: Reducing CPU usage

 
0
  #9
Jun 26th, 2006
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.
Yeah, what he said! :cheesy:
------------------------------------------------------------
If you see no coffee in my immediate vicinity, speak slowly and use small words....
ConnectNL Directory | Blog
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 6
Reputation: joe935 is an unknown quantity at this point 
Solved Threads: 0
joe935 joe935 is offline Offline
Newbie Poster

Re: Reducing CPU usage

 
0
  #10
Jun 26th, 2006
Sorry to report that Agrothe's code did not work. Merlin never appears to say the time.
Don't force it, Use a bigger hammer!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC