Solved

Timer to run a program

Posted on 2000-03-14
8
186 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 27

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 27

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now