Home > Microsoft .Net Development Tips > ARCHIVE: Tips & Tricks > Overcome timer limitations
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ARCHIVE: TIPS & TRICKS

Overcome timer limitations


Saurabh Kumar
05.22.2001
Rating: -3.78- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Overcome timer limitations
Saurabh Kumar

We have received a lot of feedback about the tip of hacking down to seconds. We plan to publish some of that feedback in the near future. But now, undeterred by the fact that some readers have taken us to task, we present this tip, submitted by reader Saurabh Kumar, that deals with timing large intervals.


A common complaint of VB programmers is the limitation of the built-in Timer control, which allows intervals only about as big as a minute. Here is a technique that I use very effectively to have intervals ranging from 1 second to as big as you wish. You can now make your software run scheduled code every 15 minutes, every 10 hours, every 2 months, whatever!

I have provided the code, which must be placed into the 'General' 'Declarations' procedure, below. You should set the interval of the Timer to 1000, so this code is executed every second.

Now just place all the code you wish to be executed repeatedly, in a procedure named 'DoIt'.

You are ready. Notice that at the end of the Timer event, this code will execute the 'DoIt' procedure every 5 minutes. You can change it easily to suit your requirement. In this line, you can use 'timeSeconds' for interval in seconds (<60), 'timeHours' for interval in hours (<24) etc.

I have also made a sample application to demonstrate how to use this code easily in your program. Use it to do automated tasks on your server, send mails, do unattended database maintenance jobs etc.

  
Private Sub Timer1_Timer()
dim BigInterval as byte
Static timeSeconds As Byte
Static timeMinutes As Byte
Static timeHours As Byte
Static timeDays As Byte
timeSeconds = timeSeconds + 1
If timeSeconds >= 60 Then
    timeSeconds = 0
    timeMinutes = timeMinutes + 1
End If
If timeMinutes >= 60 Then
    timeMinutes = 0
    timeHours = timeHours + 1
End If
If timeHours >= 24 Then
    timeHours = 0
    timeDays = timeDays + 1
End If
If timeDays >= 30 Then
    timeDays = 0
End If

'use the variable below to change it to your required interval.
'NOTE: seconds<60, minutes<60, hours<24, days<30
BigInterval=15

'remove comment from the line below to suit your unit of interval
'Seconds
'    If timeSeconds Mod BigInterval = 0 And timeSeconds <> 0 Then DoIt
'Minutes
    If timeMinutes Mod BigInterval = 0 And timeMinutes <> 0 And timeSeconds = 0 Then DoIt
'Hours
'    If timeHours Mod BigInterval = 0 And timeHours <> 0 And timeSeconds = 0 And timeMinutes=0 Then DoIt
'Days
'    If timeDays Mod BigInterval = 0 And timeDays <> 0 And timeSeconds = 0 And timeMinutes=0 And timeHours=0 Then DoIt

End Sub

Private Sub DoIt
'Add all recurring events here
msgbox "Job Executed at " & Time
End Sub

Did you like this tip? Do you have one of your own? Drop us a line.

Related Book

Advanced Visual Basic 6: Power Techniques for Everyday Programs
Author : Matthew Curland
Publisher : Addison Wesley
Published : Jul 2000
Summary :
In Advanced Visual Basic 6, leading Visual Basic expert Matthew Curland demonstrates powerhouse techniques programmers can use to write high-performance, robust, and reusable VB code. Readers learn how to leverage the close relationships between VB, COM, and OLE Automation, extending the envelope of VB's capabilities and building high-quality, object-based systems.


Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
ARCHIVE: Tips & Tricks
SearchVB wants your tips
Find out what VS.NET modules are installed
Retrieving icons from an EXE-file
Toggling Boolean values
VS.NET multiple tabbed windows
Set the value of a DataField in code behind
Easy grid design
Open using last Visual Studio.NET layout
Save development time by using inherited forms
Reusing code in VS.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts