[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Timer to run a program

Posted on 2000-03-14
8
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Accepted Solution

by:
kpcapel earned 150 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses

656 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