Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timer - run program every 5 minutes

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
Hi there :)
i've created a program that basically reads a file, checks for the word "error" and then emails sum1 with the "errors". What i need to do now is set the program to read the file every 5 minutes, which ive tried to do but i've messed up somewhere?! when the file has been read, it only sends new errors via email. here is my code:
Imports System
Imports System.IO
Imports System.Collections
Imports System.Web
Public Class Form1
Inherits System.Windows.Forms.Form
Private Shared myTimer As New System.Windows.Forms.Timer
'Private Shared exitFlag As Boolean = False
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs)
myTimer.Stop()
Dim LineNo As String
Dim CurrentLineNo As New Integer
If GetSetting("OrEM", "Settings", "LineNo") = "" Then
SaveSetting("OrEM", "Settings", "LineNo", "1")
End If
LineNo = Val(GetSetting("OrEM", "Settings", "LineNo"))
Dim objReader As New StreamReader("c:\ProjectSearch\sx3liveALRT.LOG")
Dim sLine As String = ""
Dim arrText As New ArrayList

Do
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If CurrentLineNo >= LineNo Then
If InStr(UCase(sLine), "MON") Or InStr(UCase(sLine), "TUE") Or InStr(UCase(sLine), "WED") Or InStr(UCase(sLine), "THU") Or InStr(UCase(sLine), "FRI") Or InStr(UCase(sLine), "SAT") Or InStr(UCase(sLine), "SAT") Then
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If InStr(UCase(sLine), "ERROR") Then arrText.Add(sLine)
While sLine <> &quot;&quot;
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1

End While
End If
End If
If Not sLine Is Nothing Then

End If
Loop Until sLine Is Nothing
objReader.Close()
'create mail notification
Dim mailMsg As New System.Web.Mail.MailMessage
mailMsg.BodyFormat = Mail.MailFormat.Text
mailMsg.To = &quot;???&quot;
mailMsg.Subject = &quot;???&quot;
mailMsg.From = &quot;???&quot;

Dim txtBody As String

For Each sLine In arrText
txtBody = txtBody & sLine & vbCrLf
Next

mailMsg.Body = txtBody
System.Web.Mail.SmtpMail.SmtpServer = &quot;???&quot;
System.Web.Mail.SmtpMail.Send(mailMsg)
SaveSetting(&quot;OrEm&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, Trim(Str(CurrentLineNo - 2)))
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))


' myTimer.Start()

'adds the event and the event handler for the method that will process the timer event to the timer
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
CurrentLineNo = 0
'sets the timer interval to 5 minutes
myTimer.Interval = 30000
myTimer.Start()
'runs the timer, and raises the event
' While exitFlag = False
'Processes all the events in the queue
Application.DoEvents()
'End While
End Sub

Any help much appreciated. Thanx.
 
I'm having trouble understanding what your problem is. Does this not work at all for you, currently? Or is it only executing once?
 
the program runs, doesn't appear to do anything though & i don't get any emails? (im sorry im very new to this), before i tried to run it every 5 mins, i would get an email containing the error lines. it's got to be summat to do wiv the timer?
 
Ah, I understand now.

I think your problem is either (or both) that your timer is not enabled or that you don't have your event handler registered.

Code:
Private Shared [COLOR=red]WithEvents[/color] myTimer As New System.Windows.Forms.Timer

Private Shared Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs) [COLOR=red]Handles myTimer.Tick[/color]

Then, in your [tt]New()[/tt] of the form, you can add:
Code:
myTimer.Interval = 30000
myTimer.Enabled = True

You can also get rid of the lines:
Code:
AddHandler myTimer.Tick, AddressOf TimerEventProcessor

'sets the timer interval to 5 minutes
myTimer.Interval = 30000
myTimer.Start()

Since that'll already be done at that point.
 
Don't forget that the timer in the Windows.Forms namespace is dependent on the form processing windows messages (the win32 message pump) behind the scenes.

You might want to look at the timer in the Threading namespace, as you don't need a form for that one.

There was a good article on the differences between the three timers found in the framework in a recent issue of MSDN magazine.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
i tried rosenk's method and unfortunately that still didn't work. ive also written the program as a console application - which works without the timer!! i'd also like to know how to get the program to email only when errors occur, NOT when there aren't any errors? my console app code is this:

Imports System
Imports System.IO
Imports System.Collections
Imports System.Web


Module Module1


Sub Main()

Dim myTimer As New System.Timers.Timer
Dim LineNo As String
myTimer.Stop()
Dim CurrentLineNo As New Integer
If GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;) = &quot;&quot; Then
SaveSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, &quot;1&quot;)
End If
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))
Dim objReader As New StreamReader(&quot;c:\ProjectSearch\sx3liveALRT.LOG&quot;)
Dim sLine As String = &quot;&quot;
Dim arrText As New ArrayList

Do
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If CurrentLineNo >= LineNo Then
If InStr(UCase(sLine), &quot;MON&quot;) Or InStr(UCase(sLine), &quot;TUE&quot;) Or InStr(UCase(sLine), &quot;WED&quot;) Or InStr(UCase(sLine), &quot;THU&quot;) Or InStr(UCase(sLine), &quot;FRI&quot;) Or InStr(UCase(sLine), &quot;SAT&quot;) Or InStr(UCase(sLine), &quot;SAT&quot;) Then
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If InStr(UCase(sLine), &quot;ERROR&quot;) Then arrText.Add(sLine)
While sLine <> &quot;&quot;
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1

End While
End If
End If
If Not sLine Is Nothing Then

End If
Loop Until sLine Is Nothing
objReader.Close()
'create mail notification
Dim mailMsg As New System.Web.Mail.MailMessage
mailMsg.BodyFormat = Mail.MailFormat.Text
mailMsg.To = &quot;??&quot;
mailMsg.Subject = &quot;??&quot;
mailMsg.From = &quot;??&quot;

Dim txtBody As String

For Each sLine In arrText
txtBody = txtBody & sLine & vbCrLf
Next

mailMsg.Body = txtBody
System.Web.Mail.SmtpMail.SmtpServer = &quot;??&quot;
System.Web.Mail.SmtpMail.Send(mailMsg)
SaveSetting(&quot;OrEm&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, Trim(Str(CurrentLineNo - 2)))
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))

CurrentLineNo = 0
myTimer.Start()
End Sub

End Module
 
Hi there :) Ok, the timers seems fine now, next prob is this: I want the program to email only if the file contains new &quot;errors&quot; but NOT to email if there are no new instances of the &quot;error&quot; word. so i wrapped the mail code in if sline <> &quot;&quot; then ... (mail code)...end if and it doesnt seem to mail at all now (i added errors manually to the file to check it). Could u pls tell me where im going wrong? Thanx!

Imports System
Imports System.IO
Imports System.Collections
Imports System.Web


Module Module1


Sub Main()

Dim myTimer As New System.Timers.Timer
Dim LineNo As String
myTimer.Stop()
Dim CurrentLineNo As New Integer
If GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;) = &quot;&quot; Then
SaveSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, &quot;1&quot;)
End If
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))
Dim objReader As New StreamReader(&quot;filename&quot;)
Dim sLine As String = &quot;&quot;
Dim arrText As New ArrayList

Do
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If CurrentLineNo >= LineNo Then
If InStr(UCase(sLine), &quot;MON&quot;) Or InStr(UCase(sLine), &quot;TUE&quot;) Or InStr(UCase(sLine), &quot;WED&quot;) Or InStr(UCase(sLine), &quot;THU&quot;) Or InStr(UCase(sLine), &quot;FRI&quot;) Or InStr(UCase(sLine), &quot;SAT&quot;) Or InStr(UCase(sLine), &quot;SAT&quot;) Then
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If InStr(UCase(sLine), &quot;ERROR&quot;) Then arrText.Add(sLine)
While sLine <> &quot;&quot;
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1

End While
End If
End If
If Not sLine Is Nothing Then

End If
Loop Until sLine Is Nothing
objReader.Close()

If sLine <> &quot;&quot; Then
'create mail notification
Dim mailMsg As New System.Web.Mail.MailMessage
mailMsg.BodyFormat = Mail.MailFormat.Text
mailMsg.To = &quot;me@co.uk&quot;
mailMsg.Subject = &quot;name&quot;
mailMsg.From = &quot;name&quot;

Dim txtBody As String

For Each sLine In arrText
txtBody = txtBody & sLine & vbCrLf
Next

mailMsg.Body = txtBody
System.Web.Mail.SmtpMail.SmtpServer = &quot;server&quot;
System.Web.Mail.SmtpMail.Send(mailMsg)
End If
SaveSetting(&quot;OrEm&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, Trim(Str(CurrentLineNo - 2)))
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))

CurrentLineNo = 0
myTimer.Interval = 30000
myTimer.Start()
End Sub

End Module

 
Code:
While sLine <> &quot;&quot;
    sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
End While

All this code is going to do is keep reading lines from the file until it gets to a blank line. (Even if
Code:
CurrentLineNo
becomes much greater than
Code:
LineNo
.) Is that what you intended?

Not sure what the input file really looks like, so I can't be sure if it would or wouldn't, but seems more likely that it's not what you'd want.
 
my app seems to be working fine now - thank you all for ur help :)
ive since realised that maybe i should have written the program as a windows form? because the console app runs through, does what it's supposed to do and then exits, whereas a windows form would continue to run until it is manually closed - how can i get my console app to run continuously in the background without closing? is this possible?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top