Member Avatar for joe935

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.

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.

Yup, he's right. Truth is, that's exactly what the timer control is for.

Member Avatar for joe935

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:

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
Member Avatar for joe935

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?

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:

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

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.

Member Avatar for joe935

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.

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:

Member Avatar for joe935

Sorry to report that Agrothe's code did not work. Merlin never appears to say the time.

Member Avatar for joe935

All is working now thanks to Agrothe for his help.
I changes the timer code from this:
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
to:
Private Sub Timer1_Timer()
If Minute(Now) = 15 Or Minute(Now) = 30 Or Minute(Now) = 45 Or Minute(Now) = 0 Then Saytime

And best of all CPU% is 0

Sorry to report that Agrothe's code did not work. Merlin never appears to say the time.

<cut code and explanation from here seeing as joe935 posted the solution himself seconds before I hit the post button!:o>

Good job joe!

Indeed, Good Job To Both of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.