Solved

Using the timer control

Posted on 2002-07-30
13
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 4

Accepted Solution

by:
Glowman earned 50 total points
ID: 7187801
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
ID: 7187841
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
ID: 7187851
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Glowman
ID: 7187868
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
ID: 7187894
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
ID: 7187971
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 7187980
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
ID: 7187984
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
ID: 7188141
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
ID: 7188312
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
ID: 7188335
>>> SetTimer/KillTimer, though handy in some cases, is fairly immaterial in this case

I totally disagree with this statement.
0
 

Expert Comment

by:eamonk
ID: 7188337
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
ID: 7206837
ummm Sammy???
You still alive?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 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