Solved

Using the timer control

Posted on 2002-07-30
13
835 Views
Last Modified: 2010-04-24
I need have a function that I need to run at a certain interval i.e 5 mins. I have not used the timer control before and I am looking for some code to wait a certain amount of time before running a procedure.
0
Comment
Question by:SammyG
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 4

Accepted Solution

by:
Glowman earned 50 total points
Comment Utility
Sammy,
Place the Timer control on your form and set the Interval property to 1000(1000 is one second ) and the enabled property to True.  Then in the Timer1_Timer event you will write the code that will count to 300 since there are 300 seconds in five minutes then execute your procedure, then toggle the enabled property of the timer to false, unless you want your procedure to run every five minutes.  An example is as follows.

Option Explicit
Public TimeWatch As Integer
Private Sub Timer1_Timer()
   
    If TimeWatch = 301 Then
        Call MySubRoutine
        Timer1.Enabled = False
    Else
        TimeWatch = TimeWatch + 1
    End If
   
   
End Sub

Hope this gets you started
G
0
 

Author Comment

by:SammyG
Comment Utility
Thanks for the reply.

Here is how I need this to work:

Sub DoSomething()

 If CanContinue() = False Then
  Wait 5 mins and run DoSomething Again
 Else
  'Carry on
 End If

End Sub


Function CanContinue() as Boolean
 If ...'Do something
    CanContinue = True
 Else
    CanContimue = False
 End If
End Function
0
 

Author Comment

by:SammyG
Comment Utility
Thanks for the reply.

Here is how I need this to work:

Sub DoSomething()

 If CanContinue() = False Then
  Wait 5 mins and run DoSomething Again
 Else
  'Carry on
 End If

End Sub


Function CanContinue() as Boolean
 If ...'Do something
    CanContinue = True
 Else
    CanContimue = False
 End If
End Function
0
 
LVL 4

Expert Comment

by:Glowman
Comment Utility
Sammy,

You can do it that way by declaring a boolean value and changing it to True in the Timer1_timer event in place of where we called MySubRoutine.  So then your code you could have a tight loop that waits for the boolean value to become true, like this.

Option Explicit
Public TimeWatch As Integer
public TimeToGo as Boolean
Private Sub Timer1_Timer()
   
   If TimeWatch = 300 Then
       TimeToGo=True
   Else
       TimeWatch = TimeWatch + 1
   End If
   
   
End Sub

Sub DoSomething()

If CanContinue() = False Then
 Wait 5 mins and run DoSomething Again
 
 Do until TimeToGo= true
   DoEvents
 Loop

 TimeWatch=0
 TimeToGo=False

Else
 'Carry on
End If

End Sub

So we are waiting for TimeToGo to be switched to True when the Timer hits 300, and after it does reset the TimeWatch Value back to Zero and TimeToGo to False.  Better?
0
 
LVL 4

Expert Comment

by:Nazdor
Comment Utility
Sammy, if you don't use a timer, then your program will hog the processor and appear to be frozen/hung - both of which are "very bad things"(tm)

If you're determined to go down this route, then try the Sleep API command:

Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)


eg:
If CanContinue() = False Then
  Sleep 3000000   ' = 5 mins in milliseconds




0
 

Author Comment

by:SammyG
Comment Utility
Nazdor:

I currently am using the sleep API. Problem is I would like to show a countdown to the user with an option to cancel.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
I would recommend using SetTimer and KillTimer API's for this.Here is how,just change the time interval in the example to 300000 for 5 minutes.

http://www.vbcodemagician.dk/tips/system_formlesstimer.htm

This is a better then using the timer control but remember,you must use killTimer to stop the timer as stated in the link.
0
 
LVL 4

Expert Comment

by:Glowman
Comment Utility
Sammy,

Then you will have to use the timer control.  The sleep function locks the app without any refreshing.

G
0
 
LVL 2

Expert Comment

by:manojanand
Comment Utility
Hi,

Try this


Timer1 Properties
================
Interval=5000 (1000 for 1 sec)
enabled=true

private sub message()''the procedure to be called
msgbox "Message"
end sub


private sub timer1_timer()
call message
timer1.enabled=false'if u want to stop executing

'u dont need the above line if u want the code to fire 'for every 5 min

end sub
0
 
LVL 4

Expert Comment

by:Nazdor
Comment Utility
Sounds like you need to display a small Modal form with a cancel button on (plus other info, such as your countdown) in conjuction with timers.

If you create the wait form with events, then you could raise an event to the parent form when the user presses cancel.




SetTimer/KillTimer, though handy in some cases, is fairly immaterial in this case as the ocx timer does the job without all the hassle of a timer in a global .bas which may need a reference to individual objects etc etc etc

0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
>>> SetTimer/KillTimer, though handy in some cases, is fairly immaterial in this case

I totally disagree with this statement.
0
 

Expert Comment

by:eamonk
Comment Utility
I think you will be limited to 1 minute using the Timer control, you could try the Timer APIs if you need longer intervals:

API Declarations:
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long


The timer will call the specified function at the specified interval.

Eg.
g_lTimerId = SetTimer(0, 0, (lLastTxnQueryInterval * 60000), AddressOf TimerProc)

Here TimerProc will be called every lLastTxnQueryInterval (this variable holds the interval in minutes - so we need to convert it to msecs).

To stop the timer simply call - KillTimer(0, g_lTimerId)

The TimerProc definition looks like:
Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)

Hope this helps.

0
 
LVL 4

Expert Comment

by:Glowman
Comment Utility
ummm Sammy???
You still alive?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now