473,573 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner problem with a windows service

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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

......
Jul 21 '05 #1
8 2018
Fabio,

From just looking at the code, I think that the problem lies in that your variable t in the OnStart event is declared in the wrong
place, so as soon as the OnStart event is completed, the variable will be destroyed, and with it all your threads. Move it to the
top of the class, and try it again.

One other thing, your threads will all just run once, and never again, so what you also might try doing is put in a variable that
monitors whether the service is still running, and then run your MainLoop in a loop that checks if the service is still running.
That way you won't have to stop and start your service every day.

HTH

Thys Brits
MCSD/MCSD.Net

"Fabio Papa" <fa****@hotmail .com> wrote in message news:nLZcb.32$J X.16@edtnps84.. .
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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

......

Jul 21 '05 #2
But isn't the code looping and checking for isalive , which would ensure
that the all the threads has completed execution.

"Thys Brits" <th**@jtsoftwar e.co.za> wrote in message
news:ur******** *****@TK2MSFTNG P11.phx.gbl...
Fabio,

From just looking at the code, I think that the problem lies in that your variable t in the OnStart event is declared in the wrong place, so as soon as the OnStart event is completed, the variable will be destroyed, and with it all your threads. Move it to the top of the class, and try it again.

One other thing, your threads will all just run once, and never again, so what you also might try doing is put in a variable that monitors whether the service is still running, and then run your MainLoop in a loop that checks if the service is still running. That way you won't have to stop and start your service every day.

HTH

Thys Brits
MCSD/MCSD.Net

"Fabio Papa" <fa****@hotmail .com> wrote in message news:nLZcb.32$J X.16@edtnps84.. . 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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

.....

Jul 21 '05 #3
Hi Thys,

Thanks for your help, but this didn't seem to solve my problem. Does anyone
have any other ideas out there?
"Thys Brits" <th**@jtsoftwar e.co.za> wrote in message
news:ur******** *****@TK2MSFTNG P11.phx.gbl...
Fabio,

From just looking at the code, I think that the problem lies in that your variable t in the OnStart event is declared in the wrong place, so as soon as the OnStart event is completed, the variable will be destroyed, and with it all your threads. Move it to the top of the class, and try it again.

One other thing, your threads will all just run once, and never again, so what you also might try doing is put in a variable that monitors whether the service is still running, and then run your MainLoop in a loop that checks if the service is still running. That way you won't have to stop and start your service every day.

HTH

Thys Brits
MCSD/MCSD.Net

"Fabio Papa" <fa****@hotmail .com> wrote in message news:nLZcb.32$J X.16@edtnps84.. . 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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

.....

Jul 21 '05 #4
Hi again,

Have you tried debugging the service to see where it's getting stuck? If you're having trouble debugging the service, let me/us
know.

Thys

"Fabio Papa" <fa****@hotmail .com> wrote in message news:Asjeb.2828 5$o21.24703@edt nps84...
Hi Thys,

Thanks for your help, but this didn't seem to solve my problem. Does anyone
have any other ideas out there?
"Thys Brits" <th**@jtsoftwar e.co.za> wrote in message
news:ur******** *****@TK2MSFTNG P11.phx.gbl...
Fabio,

From just looking at the code, I think that the problem lies in that your variable t in the OnStart event is declared in the wrong place, so as soon as the OnStart event is completed, the variable will be destroyed, and with it all your threads. Move it to the top of the class, and try it again.

One other thing, your threads will all just run once, and never again, so what you also might try doing is put in a variable that monitors whether the service is still running, and then run your MainLoop in a loop that checks if the service is still running. That way you won't have to stop and start your service every day.

HTH

Thys Brits
MCSD/MCSD.Net

"Fabio Papa" <fa****@hotmail .com> wrote in message news:nLZcb.32$J X.16@edtnps84.. . 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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

.....


Jul 21 '05 #5
Hi Thys,

I really appreciate you helping me out! Thanks so much. I don't really
know how to debug a windows service since I have to install the service in
order to run it. Kinda confusing. As a work-around, I have set some lines
in the code to write to the application event log. From that, I can tell
that all the code in OnStart runs (it writes to the event log before and
after the threads are supposedly started) and it even writes the name of the
cities that it retrieves from the DB. But it never runs any code from
MainLoop, which should be run 5 times (that's how many cities I have in the
DB), in 5 seperate threads. I know this because the threads never write to
the event log.

Would it be easier/better to write the mainloop method in a seperate class
and then call that method asynchronously from OnStart? Thanks again.
fp
"Thys Brits" <th**@jtsoftwar e.co.za> wrote in message
news:On******** ******@TK2MSFTN GP09.phx.gbl...
Hi again,

Have you tried debugging the service to see where it's getting stuck? If you're having trouble debugging the service, let me/us know.

Thys

"Fabio Papa" <fa****@hotmail .com> wrote in message news:Asjeb.2828 5$o21.24703@edt nps84... Hi Thys,

Thanks for your help, but this didn't seem to solve my problem. Does anyone have any other ideas out there?
"Thys Brits" <th**@jtsoftwar e.co.za> wrote in message
news:ur******** *****@TK2MSFTNG P11.phx.gbl...
Fabio,

From just looking at the code, I think that the problem lies in that your
variable t in the OnStart event is declared in the wrong
place, so as soon as the OnStart event is completed, the variable will
be destroyed, and with it all your threads. Move it to the
top of the class, and try it again.

One other thing, your threads will all just run once, and never again,
so what you also might try doing is put in a variable that
monitors whether the service is still running, and then run your
MainLoop in a loop that checks if the service is still running.
That way you won't have to stop and start your service every day.

HTH

Thys Brits
MCSD/MCSD.Net

"Fabio Papa" <fa****@hotmail .com> wrote in message

news:nLZcb.32$J X.16@edtnps84.. .
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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

.....


Jul 21 '05 #6
Hi Fabio,

There are two thngs that occur to me that you can do.

Firstly, write this as a standard application. It's not doing anything
particulalry 'servicey', like watching the system for 48 hours and amassing
data, etc. So write it as an aplication where you are in familiar territory
debugging-wise. Then, when you've got it working, take out the guts and
transplant them into a Windows Service framework.

Secondly, when working with Threads, its useful to take advantage of the
Name property. When you create your Threads, given them a name which you can
subsequently display in any debug output. This will let you know who is doing
what.

If you continue to have problems, I'm happy to take your project for a
spin - but give it a bash yourself first. Good luck. :-)

Regards,
Fergus
Jul 21 '05 #7
Hi again Fabio,

A further thought.

Yes, do move the mailing code into a class of its own. If nothing else, it
will separate the application framework from the functionality. Much cleaner.

Regards,
Fergus
Jul 21 '05 #8
Hi,

I was currently working on such situation. I am not sure what is wrong
in your code. But don't you have to wait for your threads to get
finish before you return from OnStart() method.

And make sure that OnStart() does not get called while any of the
previous thread is not finished. I can say that even now the threads
are executing Main Loop but they will not put any message in event
viewer until some time passes or until they are done. I have observed
that all the threads post message to event viewer at the same time.
And it may be happening that the MainLoop for each thread waits for
each other somehow and it may be going into infinite wait..

Put a sleep wait in OnStart() and then after some time, INTERRUPT each
thread if they are still alive. After interruption they all will
display message in event viewer.

Thanks,
piyush
"Fabio Papa" <fa****@hotmail .com> wrote in message news:<nLZcb.32$ JX.16@edtnps84> ...
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.NumL ocales - 1) As Thread
Dim log As EventLog = New EventLog()

log.WriteEntry( ServiceName, "Service started")
For i = 0 To Me.dbObj.NumLoc ales - 1
Me.index = i
t(i) = New Thread(AddressO f 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 secsFromMidnigh tToGo As Integer
Dim secsFromMidnigh tToNow As Integer
secsFromMidnigh tToGo = CInt(DateDiff(" s", CDate("12/30/1899 12:00:00
AM"), _
Me.dbObj.GoTime (i)))
secsFromMidnigh tToNow = CInt(DateDiff(" s", TimeOfDay, Today))
Dim hrs, mins, secs As Integer

Do
If secsFromMidnigh tToNow > secsFromMidnigh tToGo Then
TimeDiff = secsFromMidnigh tToNow - secsFromMidnigh tToGo
Interval = (TwentyFourHrs - TimeDiff) * 1000 'miliseconds
ElseIf secsFromMidnigh tToGo > secsFromMidnigh tToNow Then
TimeDiff = secsFromMidnigh tToGo - secsFromMidnigh tToNow
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(In terval)
logEntry = String.Format(" Sending emails for {0}...", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Me.dbObj.SendEM ails(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.SendFa xes(i)
logEntry = String.Format(" Done sending faxes for {0}", _
Me.dbObj.Locale (i))
log.WriteEntry( ServiceName, logEntry)
Loop

End Sub

.....

Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2425
by: Steve Richfie1d | last post by:
I wrote my first BASIC compiler when Bill Gates was going to Lakeside School, and am believed to be the original inventor of the ON ERROR statement. Now, my son wants to learn Visual Basic, but NOT from me. He wants a manual that is NOT a handholding tome on how to write a program, but instead is rather a complete language description in...
1
6692
by: AlexB | last post by:
The ASP.NET account needs access to the following directory: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 \Temporary ASP.NET Files Locate the folder in Windows Explorer, right-click on the file and select the Security tab. Then add the ASP.NET user and check Full Access. After you fix this for the "\Temporary ASP.NET files"
44
4159
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
14
1810
by: japin0 | last post by:
Hi, I am a absolute beginner in ASP ..I do have a bit of exposure to .VBS and other scripting languages. I need your guidance, where do I start? I had been to MS website Quick start guide but it says we need a basic web building experience to undrestand.. Can you please refer to a guide or a website which can cater my need? rgards
7
1572
by: Fabio Papa | last post by:
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...
2
1072
by: WebBuilder451 | last post by:
I need to create something that just calls a web page once a day. It needs to be automatic. My guess is that it needs to be a service(?) or can it be a program that can be setup as a service. 'need a little advice. here's a list of what i need /know 1. i need it to be run everyday at 4:00pm 2. it calls a webpage that is not on site(, but is...
14
1663
by: simchajoy2000 | last post by:
Hi, This question will probably be too simplistic for all of you, but I am trying to teach myself asp and I am having all sorts of problems. I have a simple html form which calls process.asp on submit: <html> <head></head> <body> <form action="process.asp" method=Post>
6
1537
by: xfile | last post by:
Hello, I am very new to donet and wondering how to solve the following scenario: (1) Our current hosted site has .Net 1.1 and can be upgraded to 2.0 if needed. Some downtime are expected and may have unexpected problems though. (2) Local environment installed both 1.1 and 2.0 framework and 2.0 SDK. (3) Currently, all our ASP and HTML...
0
7746
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8179
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3694
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2166
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
992
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.