Solved

Using the timer control

Posted on 2002-07-30
13
838 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 71
Paint/Redraw window while dragging 16 79
VB6 - Compare and highlight cell not the same 3 52
RUNRMTCMD from AS/400 12 47
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…

831 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