Solved

Timer to run a program

Posted on 2000-03-14
8
193 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
Industry Leaders: 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!

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…

733 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