ict-torquilclark
asked on
VB.Net Timers.Timer loop not working
I ahve written a VB.Net service that had a Timers.Timer control with an interval of 120000 (2 minutes)
I then use the attatched code to run my procedure (importlead()) and restart the timer so it runs again in 2 minutes.
It runs through fine the 1st time but it never seems to call the import lead procedure after that.
Can anybody see what i am doing wrong?
I then use the attatched code to run my procedure (importlead()) and restart the timer so it runs again in 2 minutes.
It runs through fine the 1st time but it never seems to call the import lead procedure after that.
Can anybody see what i am doing wrong?
Try
Timer.Stop()
Timer.Close()
Timer.Dispose()
ImportLead()
Catch ex As Exception
sWriter.WriteLine("Error Description: " & Err.Description)
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
Finally
Timer = New Timers.Timer
Timer.Interval = 120000
Timer.Start()
End Try
You also don't need to close/dispose. You just need to stop and then start.
Try this way:
Try this way:
Try
Timer.Stop()
ImportLead()
Catch ex As Exception
sWriter.WriteLine("Error Description: " & Err.Description)
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
Finally
Timer.Start()
End Try
ASKER
Hi Both
that is how I had it setup initially and it didnt work, it is only this morning that I have added those extra few lines in an attempt to make it work ? :o(
that is how I had it setup initially and it didnt work, it is only this morning that I have added those extra few lines in an attempt to make it work ? :o(
hi,
please provide comeplete file code, to check, what is the problem.
Thanks
please provide comeplete file code, to check, what is the problem.
Thanks
ASKER
Public Class ClickLeadImporter
Private WithEvents Timer As New Timers.Timer
Dim sWriter As New System.IO.StreamWriter("\\sql1\DORIS Tasks\Click Lead Importer Service\Log.txt")
Dim SqlCnn As String = "Provider=SQLOLEDB.1;Server=sql1;Database=lpd;Persist Security Info=False;User ID=sa;Password=starfish"
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
If System.IO.File.Exists("\\sql1\DORIS Tasks\Click Lead Importer Service\Log.txt") = False Then
System.IO.File.Create("Log.txt")
End If
Timer.Interval = 120000
Timer.Start()
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
Try
Timer.Stop()
Timer.Close()
Timer.Dispose()
ImportLead()
Catch ex As Exception
sWriter.WriteLine("Error Description: " & Err.Description)
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
Finally
Timer = New Timers.Timer
Timer.Interval = 120000
Timer.Start()
End Try
End Sub
Private Sub ImportLead()
Dim DirectoryInfo As New System.IO.DirectoryInfo("\\tcweb\Leads\Click")
Dim FileArray As System.IO.FileInfo() = DirectoryInfo.GetFiles("*.txt")
Dim file As System.IO.FileInfo
sWriter.WriteLine("--------------------")
sWriter.WriteLine("Lead Check Started at: " & Now.ToString)
If FileArray.Length = 0 Then
sWriter.WriteLine("No New Leads")
End If
For Each file In FileArray
sWriter.WriteLine("File " & file.Name & " Found")
Dim sReader As New System.IO.StreamReader(file.FullName)
Dim leadContent As String
leadContent = sReader.ReadLine
Dim Title As String = leadContent.Replace("Title:", "")
leadContent = sReader.ReadLine
Dim Initials As String = leadContent.Replace("Initials:", "")
leadContent = sReader.ReadLine
Dim Surname As String = leadContent.Replace("Surname:", "")
leadContent = sReader.ReadLine
Dim Address1 As String = leadContent.Replace("Address1:", "")
leadContent = sReader.ReadLine
Dim Address2 As String = leadContent.Replace("Address2:", "")
leadContent = sReader.ReadLine
Dim Address3 As String = leadContent.Replace("Address3:", "")
leadContent = sReader.ReadLine
Dim Address4 As String = leadContent.Replace("Address4:", "")
leadContent = sReader.ReadLine
Dim Address5 As String = leadContent.Replace("Address5:", "")
leadContent = sReader.ReadLine
Dim PostCode As String = leadContent.Replace("Postcode:", "")
leadContent = sReader.ReadLine
Dim DOB As String = leadContent.Replace("DOB:", "")
leadContent = sReader.ReadLine
Dim Sex As String = leadContent.Replace("Sex:", "")
leadContent = sReader.ReadLine
Dim Smoker As Boolean
If leadContent.Replace("Smoker:", "").Length <> 0 Then
If leadContent.Replace("Smoker:", "").Substring(0, 1).ToUpper = "Y" Then
Smoker = True
Else
Smoker = False
End If
End If
leadContent = sReader.ReadLine
Dim Telephone1 As String = leadContent.Replace("Telephone1:", "")
leadContent = sReader.ReadLine
Dim Telephone2 As String = leadContent.Replace("Telephone2:", "")
leadContent = sReader.ReadLine
Dim Telephone3 As String = leadContent.Replace("Telephone3:", "")
leadContent = sReader.ReadLine
Dim Email As String = leadContent.Replace("Email:", "")
leadContent = sReader.ReadLine
Dim PartnerDOB As String = leadContent.Replace("PartnerDOB:", "")
leadContent = sReader.ReadLine
Dim PartnerSex As String = leadContent.Replace("PartnerSex:", "")
leadContent = sReader.ReadLine
Dim PartnerSmoker As Boolean
If leadContent.Replace("PartnerSmoker:", "").Length > 0 Then
If leadContent.Replace("PartnerSmoker:", "").Substring(0, 1).ToUpper = "Y" Then
PartnerSmoker = True
Else
PartnerSmoker = False
End If
End If
leadContent = sReader.ReadLine
Dim LivesAssured As String = leadContent.Replace("LivesAssured:", "")
leadContent = sReader.ReadLine
Dim CoverType As String = leadContent.Replace("CoverType:", "")
leadContent = sReader.ReadLine
Dim CIC As Boolean
If leadContent.Replace("CIC:", "").Length <> 0 Then
If leadContent.Replace("CIC:", "").Substring(0, 1).ToUpper = "Y" Then
CIC = True
Else
CIC = False
End If
End If
leadContent = sReader.ReadLine
Dim Term As Integer = leadContent.Replace("Term:", "")
leadContent = sReader.ReadLine
Dim SumAssured As Long = leadContent.Replace("SumAssured:", "")
sReader.Close()
Dim rs As New ADODB.Recordset
Dim sqlstr As String
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
sqlstr = "SELECT * FROM WebEnquiries WHERE WebEnquiryID = 0"
rs.Open(sqlstr, SqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
rs.AddNew()
rs.Fields("SourceID").Value = "255"
rs.Fields("ID").Value = "1"
rs.Fields("Sex").Value = Sex
rs.Fields("Title").Value = Title
rs.Fields("Initials").Value = Initials
rs.Fields("Surname").Value = Surname
rs.Fields("Address1").Value = Address1
rs.Fields("Address2").Value = Address2
rs.Fields("Address3").Value = Address3
rs.Fields("Address4").Value = Address4
rs.Fields("PostCode").Value = PostCode
rs.Fields("DOB").Value = DOB
rs.Fields("Smoker").Value = Smoker
rs.Fields("Daytime_Telephone").Value = Telephone1
rs.Fields("Evening_Telephone").Value = Telephone2
rs.Fields("Mobile_Telephone").Value = Telephone3
rs.Fields("Email").Value = Email
rs.Fields("Hear").Value = "255"
rs.Fields("DateU").Value = Date.Now
rs.Fields("DateStamp").Value = Date.Now
rs.Fields("CallCount").Value = 0
rs.Fields("P_Sex").Value = PartnerSex
rs.Fields("P_Smoker").Value = PartnerSmoker
rs.Fields("P_DOB").Value = PartnerDOB
rs.Fields("CoverSingleJoint1").Value = LivesAssured
rs.Fields("CoverType1").Value = CoverType
rs.Fields("CoverCIC1").Value = CIC
rs.Fields("CoverTerm1").Value = Term
rs.Fields("CoverAmount1").Value = SumAssured
rs.Update()
rs.Close()
System.IO.File.Move(file.FullName, file.DirectoryName & "\Imported\" & file.Name)
sWriter.WriteLine("File " & file.Name & " Processed")
Next
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
End Sub
End Class
This code doesn't have the suggested changes. also you should close the sWriter only in the ImportLead method and not everywhere. Implement a try catch block on the ImportLead() and not in the Timer_Elapsed event.
Hi,
I have changed the code a little please test it now, and make sure that log file is updated
I have changed the code a little please test it now, and make sure that log file is updated
Public Class ClickLeadImporter
Private WithEvents Timer As New Timers.Timer
Dim sWriter As New System.IO.StreamWriter("\\sql1\DORIS Tasks\Click Lead Importer Service\Log.txt")
Dim SqlCnn As String = "Provider=SQLOLEDB.1;Server=sql1;Database=lpd;Persist Security Info=False;User ID=sa;Password=starfish"
Protected Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
If System.IO.File.Exists("\\sql1\DORIS Tasks\Click Lead Importer Service\Log.txt") = False Then
System.IO.File.Create("Log.txt")
End If
Timer.Interval = 120000
Timer.Start()
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
Try
sWriter.WriteLine("Stoping Timmer : " + DateTime.Now.ToString())
Timer.Stop()
sWriter.WriteLine("Stoped Timmer : " + DateTime.Now.ToString())
ImportLead()
Catch ex As Exception
sWriter.WriteLine("Error Description: " & Err.Description)
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
Finally
sWriter.WriteLine("Starting Timmer : " + DateTime.Now.ToString())
Timer.Start()
sWriter.WriteLine("Started Timmer : " + DateTime.Now.ToString())
End Try
End Sub
Private Sub ImportLead()
Dim DirectoryInfo As New System.IO.DirectoryInfo("\\tcweb\Leads\Click")
Dim FileArray As System.IO.FileInfo() = DirectoryInfo.GetFiles("*.txt")
Dim file As System.IO.FileInfo
sWriter.WriteLine("--------------------")
sWriter.WriteLine("Lead Check Started at: " & Now.ToString)
If FileArray.Length = 0 Then
sWriter.WriteLine("No New Leads")
End If
For Each file In FileArray
sWriter.WriteLine("File " & file.Name & " Found")
Dim sReader As New System.IO.StreamReader(file.FullName)
Dim leadContent As String
leadContent = sReader.ReadLine
Dim Title As String = leadContent.Replace("Title:", "")
leadContent = sReader.ReadLine
Dim Initials As String = leadContent.Replace("Initials:", "")
leadContent = sReader.ReadLine
Dim Surname As String = leadContent.Replace("Surname:", "")
leadContent = sReader.ReadLine
Dim Address1 As String = leadContent.Replace("Address1:", "")
leadContent = sReader.ReadLine
Dim Address2 As String = leadContent.Replace("Address2:", "")
leadContent = sReader.ReadLine
Dim Address3 As String = leadContent.Replace("Address3:", "")
leadContent = sReader.ReadLine
Dim Address4 As String = leadContent.Replace("Address4:", "")
leadContent = sReader.ReadLine
Dim Address5 As String = leadContent.Replace("Address5:", "")
leadContent = sReader.ReadLine
Dim PostCode As String = leadContent.Replace("Postcode:", "")
leadContent = sReader.ReadLine
Dim DOB As String = leadContent.Replace("DOB:", "")
leadContent = sReader.ReadLine
Dim Sex As String = leadContent.Replace("Sex:", "")
leadContent = sReader.ReadLine
Dim Smoker As Boolean
If leadContent.Replace("Smoker:", "").Length <> 0 Then
If leadContent.Replace("Smoker:", "").Substring(0, 1).ToUpper = "Y" Then
Smoker = True
Else
Smoker = False
End If
End If
leadContent = sReader.ReadLine
Dim Telephone1 As String = leadContent.Replace("Telephone1:", "")
leadContent = sReader.ReadLine
Dim Telephone2 As String = leadContent.Replace("Telephone2:", "")
leadContent = sReader.ReadLine
Dim Telephone3 As String = leadContent.Replace("Telephone3:", "")
leadContent = sReader.ReadLine
Dim Email As String = leadContent.Replace("Email:", "")
leadContent = sReader.ReadLine
Dim PartnerDOB As String = leadContent.Replace("PartnerDOB:", "")
leadContent = sReader.ReadLine
Dim PartnerSex As String = leadContent.Replace("PartnerSex:", "")
leadContent = sReader.ReadLine
Dim PartnerSmoker As Boolean
If leadContent.Replace("PartnerSmoker:", "").Length > 0 Then
If leadContent.Replace("PartnerSmoker:", "").Substring(0, 1).ToUpper = "Y" Then
PartnerSmoker = True
Else
PartnerSmoker = False
End If
End If
leadContent = sReader.ReadLine
Dim LivesAssured As String = leadContent.Replace("LivesAssured:", "")
leadContent = sReader.ReadLine
Dim CoverType As String = leadContent.Replace("CoverType:", "")
leadContent = sReader.ReadLine
Dim CIC As Boolean
If leadContent.Replace("CIC:", "").Length <> 0 Then
If leadContent.Replace("CIC:", "").Substring(0, 1).ToUpper = "Y" Then
CIC = True
Else
CIC = False
End If
End If
leadContent = sReader.ReadLine
Dim Term As Integer = leadContent.Replace("Term:", "")
leadContent = sReader.ReadLine
Dim SumAssured As Long = leadContent.Replace("SumAssured:", "")
sReader.Close()
Dim rs As New ADODB.Recordset
Dim sqlstr As String
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
sqlstr = "SELECT * FROM WebEnquiries WHERE WebEnquiryID = 0"
rs.Open(sqlstr, SqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
rs.AddNew()
rs.Fields("SourceID").Value = "255"
rs.Fields("ID").Value = "1"
rs.Fields("Sex").Value = Sex
rs.Fields("Title").Value = Title
rs.Fields("Initials").Value = Initials
rs.Fields("Surname").Value = Surname
rs.Fields("Address1").Value = Address1
rs.Fields("Address2").Value = Address2
rs.Fields("Address3").Value = Address3
rs.Fields("Address4").Value = Address4
rs.Fields("PostCode").Value = PostCode
rs.Fields("DOB").Value = DOB
rs.Fields("Smoker").Value = Smoker
rs.Fields("Daytime_Telephone").Value = Telephone1
rs.Fields("Evening_Telephone").Value = Telephone2
rs.Fields("Mobile_Telephone").Value = Telephone3
rs.Fields("Email").Value = Email
rs.Fields("Hear").Value = "255"
rs.Fields("DateU").Value = Date.Now
rs.Fields("DateStamp").Value = Date.Now
rs.Fields("CallCount").Value = 0
rs.Fields("P_Sex").Value = PartnerSex
rs.Fields("P_Smoker").Value = PartnerSmoker
rs.Fields("P_DOB").Value = PartnerDOB
rs.Fields("CoverSingleJoint1").Value = LivesAssured
rs.Fields("CoverType1").Value = CoverType
rs.Fields("CoverCIC1").Value = CIC
rs.Fields("CoverTerm1").Value = Term
rs.Fields("CoverAmount1").Value = SumAssured
rs.Update()
rs.Close()
System.IO.File.Move(file.FullName, file.DirectoryName & "\Imported\" & file.Name)
sWriter.WriteLine("File " & file.Name & " Processed")
Next
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
End Sub
End Class
ASKER
OK I have made the changes suggested
here is the log file after the service has been running for 5 minutes
Stoping Timmer : 31/03/2011 10:31:21
Stoped Timmer : 31/03/2011 10:31:21
--------------------
Lead Check Started at: 31/03/2011 10:31:21
File 20110331085501-Balzer.txt Found
File 20110331085501-Balzer.txt Processed
File 20110331090501-Morris.txt Found
File 20110331090501-Morris.txt Processed
Lead Check Finished at: 31/03/2011 10:31:21
here is the log file after the service has been running for 5 minutes
Stoping Timmer : 31/03/2011 10:31:21
Stoped Timmer : 31/03/2011 10:31:21
--------------------
Lead Check Started at: 31/03/2011 10:31:21
File 20110331085501-Balzer.txt Found
File 20110331085501-Balzer.txt Processed
File 20110331090501-Morris.txt Found
File 20110331090501-Morris.txt Processed
Lead Check Finished at: 31/03/2011 10:31:21
So, is it working fine?
ok, I got it your service is throwing execption due to file looger as you are closing file handler in , you can check your eventviewer log too for this error.
Private Sub ImportLead()
......
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close() ' here is the problem
End Sub
change it too
Private Sub ImportLead()
......
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
End Sub
agter this retest the code
Thanks
Private Sub ImportLead()
......
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close() ' here is the problem
End Sub
change it too
Private Sub ImportLead()
......
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
End Sub
agter this retest the code
Thanks
ASKER
I will try that now
It isnt working - it should inportlead() every 2 minutes...
It isnt working - it should inportlead() every 2 minutes...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ok
I have made that change and it is always working perfectly!!!
it is now running through the import lead procedure but it either isnt updating the log file at all, or it is overwriting what is already in it every thime - i want a continous log file...
I have made that change and it is always working perfectly!!!
it is now running through the import lead procedure but it either isnt updating the log file at all, or it is overwriting what is already in it every thime - i want a continous log file...
just change this line
Dim sWriter As New System.IO.StreamWriter("\\ sql1\DORIS Tasks\Click Lead Importer Service\Log.txt")
to
Dim sWriter As New System.IO.StreamWriter("\\ sql1\DORIS Tasks\Click Lead Importer Service\Log.txt", True)
Dim sWriter As New System.IO.StreamWriter("\\
to
Dim sWriter As New System.IO.StreamWriter("\\
ASKER
i think i am getting there with this. the only problem is that the text file doesnt update until thservie is stopeed
that is why I was using swriter.close
does that make sense?
that is why I was using swriter.close
does that make sense?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Great thanks for all of your help...
Do you both aggree that I should split the points between the two of you equally?
Do you both aggree that I should split the points between the two of you equally?
>> Do you both aggree that I should split the points between the two of you equally?
No problem for me ;)
No problem for me ;)
you don't have to reinitialize the timmer again just remove follwoing lines from finally
Timer = New Timers.Timer
Timer.Interval = 120000
and your code will look like this.
Try
Timer.Stop()
Timer.Close()
Timer.Dispose()
ImportLead()
Catch ex As Exception
sWriter.WriteLine("Error Description: " & Err.Description)
sWriter.WriteLine("Lead Check Finished at: " & Now.ToString)
sWriter.Close()
Finally
Timer.Start()
End Try
Thanks