944,149 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 25th, 2006
0

Reducing CPU usage

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe935 is offline Offline
6 posts
since Jun 2006
Jun 25th, 2006
0

Re: Reducing CPU usage

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.
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

Yup, he's right. Truth is, that's exactly what the timer control is for.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 26th, 2006
0

Re: Reducing CPU usage

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe935 is offline Offline
6 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe935 is offline Offline
6 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

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
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 26th, 2006
0

Re: Reducing CPU usage

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe935 is offline Offline
6 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

Quote 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:
Reputation Points: 37
Solved Threads: 18
Junior Poster
agrothe is offline Offline
151 posts
since Jun 2006
Jun 26th, 2006
0

Re: Reducing CPU usage

Sorry to report that Agrothe's code did not work. Merlin never appears to say the time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joe935 is offline Offline
6 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Text to Speech
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Include datafield into the section 4 of the Datareport





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC