F
Fabio Papa
I am trying to write a windows service that sends emails to clients at
specific times based on information in a sql db. Since this is done for
multiple cities, I start a thread for each city and continue the processing
from each thread. My service starts fine (gives me no errors, etc), but it
doesn't seem to start the new threads. I am new to windows services, so I
don't know if I'm doing something wrong. Should I maybe be doing this with
events? Below is the code for the OnStart Method and the thread that is
supposed to start multiple times. If you need the rest of the code, email
me and I will send to you. Thanks.
Public MustInherit Class EmailPump : Inherits ServiceBase
Public Const TwentyFourHrs As Long = 86400 'There are 86,400 seconds in
24 hours
Private name As String
Private index As Integer
Private dbObj As RgsDbConnection
......
Protected Overrides Sub OnStart(ByVal args() As String)
Dim i As Integer
Dim t(Me.dbObj.NumLocales - 1) As Thread
Dim log As EventLog = New EventLog()
log.WriteEntry(ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLocales - 1
Me.index = i
t(i) = New Thread(AddressOf MainLoop)
t(i).Start()
'spin until thread starts
While Not (t(i).IsAlive)
End While
'yield to other threads
Thread.Sleep(0)
Dim logEntry As String
logEntry = String.Format("Waiting to send emails and " _
+ "faxes for {0}.", dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Next i
End Sub
Public Sub MainLoop()
Dim TimeDiff As Integer
Dim Interval As Integer
Dim i As Integer = Me.index
Dim secsFromMidnightToGo As Integer
Dim secsFromMidnightToNow As Integer
secsFromMidnightToGo = CInt(DateDiff("s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime(i)))
secsFromMidnightToNow = CInt(DateDiff("s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer
Do
If secsFromMidnightToNow > secsFromMidnightToGo Then
TimeDiff = secsFromMidnightToNow - secsFromMidnightToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnightToGo > secsFromMidnightToNow Then
TimeDiff = secsFromMidnightToGo - secsFromMidnightToNow
Interval = TimeDiff * 1000
End If
Dim log As EventLog = New EventLog()
Dim logEntry As String
secs = ((Interval / 1000) Mod 3600) Mod 60
mins = (((Interval / 1000) - secs) Mod 3600) / 60
hrs = ((Interval / 1000) - secs - (mins * 60)) / 3600
logEntry = String.Format("Waiting for {0}hrs, {1}min " _
+ "and {2} sec to send emails and faxes for {3}.", _
hrs, mins, secs, Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Thread.Sleep(Interval)
logEntry = String.Format("Sending emails for {0}...", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Me.dbObj.SendEMails(i)
logEntry = String.Format("Done sending emails for {0}", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
logEntry = String.Format("Sending faxes for {0}...", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Me.dbObj.SendFaxes(i)
logEntry = String.Format("Done sending faxes for {0}", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Loop
End Sub
......
specific times based on information in a sql db. Since this is done for
multiple cities, I start a thread for each city and continue the processing
from each thread. My service starts fine (gives me no errors, etc), but it
doesn't seem to start the new threads. I am new to windows services, so I
don't know if I'm doing something wrong. Should I maybe be doing this with
events? Below is the code for the OnStart Method and the thread that is
supposed to start multiple times. If you need the rest of the code, email
me and I will send to you. Thanks.
Public MustInherit Class EmailPump : Inherits ServiceBase
Public Const TwentyFourHrs As Long = 86400 'There are 86,400 seconds in
24 hours
Private name As String
Private index As Integer
Private dbObj As RgsDbConnection
......
Protected Overrides Sub OnStart(ByVal args() As String)
Dim i As Integer
Dim t(Me.dbObj.NumLocales - 1) As Thread
Dim log As EventLog = New EventLog()
log.WriteEntry(ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLocales - 1
Me.index = i
t(i) = New Thread(AddressOf MainLoop)
t(i).Start()
'spin until thread starts
While Not (t(i).IsAlive)
End While
'yield to other threads
Thread.Sleep(0)
Dim logEntry As String
logEntry = String.Format("Waiting to send emails and " _
+ "faxes for {0}.", dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Next i
End Sub
Public Sub MainLoop()
Dim TimeDiff As Integer
Dim Interval As Integer
Dim i As Integer = Me.index
Dim secsFromMidnightToGo As Integer
Dim secsFromMidnightToNow As Integer
secsFromMidnightToGo = CInt(DateDiff("s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime(i)))
secsFromMidnightToNow = CInt(DateDiff("s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer
Do
If secsFromMidnightToNow > secsFromMidnightToGo Then
TimeDiff = secsFromMidnightToNow - secsFromMidnightToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnightToGo > secsFromMidnightToNow Then
TimeDiff = secsFromMidnightToGo - secsFromMidnightToNow
Interval = TimeDiff * 1000
End If
Dim log As EventLog = New EventLog()
Dim logEntry As String
secs = ((Interval / 1000) Mod 3600) Mod 60
mins = (((Interval / 1000) - secs) Mod 3600) / 60
hrs = ((Interval / 1000) - secs - (mins * 60)) / 3600
logEntry = String.Format("Waiting for {0}hrs, {1}min " _
+ "and {2} sec to send emails and faxes for {3}.", _
hrs, mins, secs, Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Thread.Sleep(Interval)
logEntry = String.Format("Sending emails for {0}...", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Me.dbObj.SendEMails(i)
logEntry = String.Format("Done sending emails for {0}", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
logEntry = String.Format("Sending faxes for {0}...", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Me.dbObj.SendFaxes(i)
logEntry = String.Format("Done sending faxes for {0}", _
Me.dbObj.Locale(i))
log.WriteEntry(ServiceName, logEntry)
Loop
End Sub
......