Solved

Timer to run a program

Posted on 2000-03-14
8
192 Views
Last Modified: 2012-05-04
I have a program which I need to continuously loop by using a timer or whatever I need to make this happen. I have a program that monitors an error log and move that file to a different folder. I need for the program to keep monitoring even though there is no more error log file. The error log file folder will keep updating itself every 30 minutes. Is there a way for me to keep my program running to monitor my log file?
0
Comment
Question by:gabedc
8 Comments
 
LVL 2

Expert Comment

by:mflam
ID: 2617626
There's a timer object in VB.
You set the interval, and turn it on/off by setting it's "enabled" true or false.
You add it to your form.
Double click it, and write what you want should happen when the "timer event" is "fired".
Here you can put code that checks if the file exists, moves it to another folder (creates the new folder) opens the logfile and appends to it, or does anything else you wish the program to do.

HTH, Moshe
0
 

Author Comment

by:gabedc
ID: 2617660
Could you please give me a sample of how I should write the Timer program. I know it should look like this:
Private Sub Timer1_Timer()
what should I write here to make sure the loop doesn't end.

End Sub
0
 

Expert Comment

by:kpcapel
ID: 2617734
You don't need to write code to make sure the loop doesn't end, its built in.
Set the interval property of the timer control to the number of milliseconds you'd like between calls to the timer event.  Just insert the code which checks your log file into the timer event.  It will execute at the interval you set as long as the form which contains the timer is loaded and the enabled property is true.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Accepted Solution

by:
kpcapel earned 50 total points
ID: 2617749
Simple code...

'Set the Enabled property to True and the Interval property to the interval you want
'You could do this directly through the property page for the control rather than in the Form_Load event
Private Sub Form_Load()
  Timer1.Enabled = True
  Timer1.Interval = 10000  '10 seconds
End Sub
'Insert your code into the Timer event
Private Sub Timer1_Timer()
  'Code for checking your logfile goes here
End Sub

You can turn off the loop by setting "Timer1.Enabled = False" at any time, or by unloading the form which contains the Timer control.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2617752
Option Explicit

Private Sub Form_Load()
    Timer1.Interval = 60000 '1 minute
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    'This code will execute every minute
    Dim sFile As String
    sFile = "c:\test.log"
    On Error Resume Next
    If Dir(sFile, vbNormal) <> "" Then
        Name sFile As "d:\back.log"
    End If
End Sub
0
 
LVL 2

Expert Comment

by:johnny6
ID: 2618008
Option Explicit
Dim iElapsedMin As Integer
Const cMax_Min As Integer = 30 'Set the maximum number of minutes for the interval here.

Private Sub Form_Load()
Timer1.Enable = True
iElapsedMin = 1
End Sub

Private Sub Timer1_Timer()
lblText.Visible = (iElapsedMin = cMax_Min)
Timer1.Enabled = (iElasedMin < CMax_Min)
iElapsedMin = iElapsedMin + 1
End Sub



Here is another solution which involves using the static variable:


Sub Timer1_Timer()
  Static TimerTimes As Integer
  TimerTimes = TimerTimes + 1
  If TimerTimes = 30 Then 'Number of minutes for interval.
    TimerTimes = 0  'reset counter
    'Add your once an hour code here.
  Else
    Exit Sub
  End If
End Sub


Make sure you set the Interval property of the timer to 30000 in whichever example you use.

John
0
 
LVL 28

Expert Comment

by:Ark
ID: 2618263
Hi
You need not to make loop - Windows do it itself. You only need to receive change notification. Take a look at http://www.mvps.org/vbnet/code/fileapi/watchedfolder.htm
Cheers
0
 
LVL 28

Expert Comment

by:Ark
ID: 2618276
Hi
This sample is better for your purpose
http://www.mvps.org/vbnet/code/shell/shchangenotify.htm
Cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

856 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