Consulting

Results 1 to 8 of 8

Thread: An Idea For A Sixty Second Timer

  1. #1

    An Idea For A Sixty Second Timer

    Hi - I have been making an activity that gives a person 60 seconds to complete a task. At the moment I have done this by having a sub-routine that runs a wait function (code below). In case you don't know this procedure takes 60 seconds to run and thereafter the rest of the original sub-routine code is run which contains an end sequence. The Wait function allows other codes and processes to run as it uses the DoEvents command (which take place through clicks of other buttons). My observation is, this works excellently on a Macintosh, but less so on Windows machines. One Windows machine it takes approximately one second for a user's click on a tile to be shown on the screen. Meanwhile, on another slightly older Windows machine the cursor permanently changes to a circle that goes around and a around! (I note that if I take the wait command out then the program runs very fast which shows that this function is noticeably slowing the machine down.)

    My question is, is there another way of having a sixty second timer which will automatically trigger some code when the time is up? I want it to work on both Macintosh and Windows - not just one of them!

    Thank you for any ideas!


    Sub Start() 'This sub-routine creates the game and then sets the 60 second timer by calling the "Wait" function
    
        ActivePresentation.SlideShowWindow.View.Next
    
        Wait 'this is a function that runs a 60 second timer
    
        'I note that when 60 seconds has passed the wait function finishes and the rest of the code within this sub-routine is run!
    
    End Sub
    
    
    Sub Wait() 'this is a 59 second timer
        Dim waitTime As Long
        Dim Start As Long
        waitTime = 59
        Start = Timer
        While Timer < Start + waitTime
            DoEvents
        Wend
    End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Since Class Modules run in their own Threads, place your timer in a Class with the properties "Time To wait" and the Method "Run"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    SamT - thank you for posting. I have only coded in PowerPoint VBA for a few weeks. I would be grateful for further explanation as to how this is carried out.

    I have a Module1 in my VBAProject (which contains my code for the project). Are you stating that I should insert a new class module within the project?
    When you write place my timer in the class - I believe you are stating that the code for the timer (the Wait function) should go in this new class?
    How do I set the properties "Time to wait" and the Method "Run"?
    How are the Time to Wait and Run codes accessed by the program as a whole?

    Thank you very much for any comments!
    Alec Armstrong

  4. #4
    Hi - I have had an experiment and created a Class Module. I named the Class Module "ClassTimer". Below is the code I used in this new class and also to access it in the main module (Module1). I would be grateful if someone could tell me if that is what SamT had in mind. The code works, although I am not sure that the outcome results in an improvement in speed performance.

    Thank you for any comments!

    'This is the code for the Class Module "ClassTimer"
    Option Explicit
    Public timerCheck As Boolean 
    
    
    Sub Run()
    
    
    timerCheck = False
    Wait
    timerCheck = True
    
    
    EndExecute 'This is a function in my Module1 which runs the code for when time runs out.
    
    
    End Sub
    
    
    Sub Wait()
        Dim waitTime As Long
        Dim Start As Long
        waitTime = 59
        Start = Timer
        While Timer < Start + waitTime
            DoEvents
        Wend
    End Sub
    Below are the lines in Module1 which activate the class:

    Dim ClassTimer2 As ClassTimer
    Set ClassTimer2 = New ClassTimer
    
    
    ActivePresentation.SlideShowWindow.View.Next 'this is my game screen
    
    
    ClassTimer2.Run

  5. #5
    Hi - I thought I would update my status with this issue. I wanted a 60 second timer without using the above wait procedure. My solution was to use the animation timeline (!) and then bring up other shapes on the screen when the time is up. During this time of identifying a solution I also learnt how to use the interactive sequences timeline which helped. To me it is very noticeable that PowerPoint works much better when the timeline is used.


    SamT provided a post suggesting the use of classes. I have not previously used classes (with VBA) and my attempt resulted in no performance gain (if anything the opposite). So, unless someone is kind enough to explain how classes can be used then I suggest this thread is closed!

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I looked at Ms's Powerpoint Object Model searching for any object which can handle an Event. All I could Find was UserForm Controls and PP application itself. Rather than trying to define a "WithEvents" App. I imagined this kludge. UserForms are a type of Class Module, one that can directly interact with the application.

    Create a UserForm with one Command Button, The Code in the UserForm is only
    Option Explicit
    
    Private Sub CommandButton1_Click()
     'Place the code you want to run after the timer function in here
    End Sub
    The TimerClass module code will look like this, based on your own Timer code

    Option Explicit
    
    Dim Trigger As Object
    Dim mWaitTime As Long
    Dim mRun As Boolean
    
    Private Sub Class_Initialize()
       Set Trigger = UserForm1
       Load Trigger
    End Sub
    
    Public Property Let WaitTime(TimetoWait As Long)
       mWaitTime = TimetoWait
    End Property
    
    Public Function StartTimer()
       mRun = True
       RunTimer
    End Function
    
    Private Sub RunTimer(()
        Dim Starttime As Long
        Starttime = Timer
        While Timer < Start + mWaitTime
            If Not mRun Then Exit Sub
        Wend
    
    Trigger.CommandButton1_Click    'May need to use Trigger.CommandButton1.Click
    End Sub
    
    Private Sub Class_Terminate()
       Unload Trigger
    End Sub

    In your code
    Dim ClassTimer2 As ClassTimer
    Set ClassTimer2 = New ClassTimer
    
    
    ActivePresentation.SlideShowWindow.View.Next 'this is my game screen
    
    ClassTimer2.WaitTime = 59
    ClassTimer2.Runtimer
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    SamT - hi and thank you for taking the time to write this. I spent some time making a blank presentation with the goal of displaying a message box in the command button. I cannot get the command button code to be processed. I note that when I try running the ClassTimer2 code in the Visual Basic browser then the UserForm comes into view.

    Otherwise I note one needs to call StartTimer and not RunTimer as mRun needs to be set as True before RunTimer will function.

    I note that without the DoEvents command that the mouse cursor changes to a circle that rotates around for the entire time the timer runs! (It looks like the whole program is focused on waiting for the while loop to finish!) However, if the DoEvents is put back in the while loop then the cursor stays the same and everything seems to run smoothly. However, I think the fact that the mouse cursor without the DoEvent caused the program to go slower, that this probably suggests that the computer will not make any performance gains compared to before?
    Last edited by ajarmstron; 07-29-2020 at 03:42 PM.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I'm lost, AJ, I have never played with PP before.

    You might look at "OnTime", where an external timer triggers|runs a sub in your project
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •