Windows Service, timer doesn't tick

M

Morten Snedker

Others had the same problem and I understand that I have to use the
System.Timers.Timer instead for the one for forms. So that's what I
do, but still it doesn't trigger:

'--code begin
Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)

With myTimer
.Interval = 10000
.Enabled = True
.Start()
End With

LogFile(Now & ": " & vbTab & "Service started")

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop
your service.
LogFile(Now & ": " & vbTab & "Service stopped")
End Sub

Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

LogFile(Now & ": " & vbTab & "Timer trigger - do nothing")

If Minute(Now) = 33 Then
LogFile(Now & ": " & vbTab & "Timer trigger - run code")
If bTrigger = False Then ExecuteWOL()
End If

End Sub
....
'---code end

So, the file should be written to every 10 seconds, but it doesn't.
The file is correctly written to upon start/stop of service.

Any ideas?

Regards /Snedker
 
I

Izzy

I would abondon the timer method and use
System.Threading.Thread.Sleep(10000) on a do loop.

If you use this method you'll need to create a new thread and put your
code is another sub, not in OnStart.

Something like this:

******************************************************************************

Public Class Service1

Private thWorker As New System.Threading.Thread(AddressOf
DoSomething)
Private bTrigger As Boolean = False

Protected Overrides Sub OnStart(ByVal args() As String)

LogFile(Now & ": " & vbTab & "Service started")
thWorker.Start()
End Sub

Protected Overrides Sub OnStop()

LogFile(Now & ": " & vbTab & "Service stopped")
thWorker.Abort()
End Sub

Private Sub DoSomething()

Do
thWorker.Sleep(10000)

LogFile(Now & ": " & vbTab & "Timer trigger - do nothing")

If Minute(Now) = 33 Then
LogFile(Now & ": " & vbTab & "Timer trigger - run code")
If bTrigger = False Then ExecuteWOL()
End If
Loop
End Sub

End Class

*****************************************************************************

Izzy
 
P

PGC

Hi Morten,

Maybe I've missed something in the code but it looks like you haven't got
either an AddHandler or Handles clause for the myTimer_Tick event. This
could be the problem.
e.g.
Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myTimer.Tick


PGC
 
C

Chris Dunaway

Morten said:
Others had the same problem and I understand that I have to use the
System.Timers.Timer instead for the one for forms. So that's what I
do, but still it doesn't trigger:

'--code begin
Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)

With myTimer
.Interval = 10000
.Enabled = True
.Start()
End With

LogFile(Now & ": " & vbTab & "Service started")

End Sub

What does the LogFile method do? Do you see the message that says
"Service Started" ? Does the service have the necessary permissions to
write to the log file?


<snip>
 
M

Morten Snedker

On 25 Sep 2006 06:48:40 -0700, "Izzy" <[email protected]>
wrote:

Thanks for your response that put me on the right track. The working
solution:

Public Class Service1

Protected Overrides Sub OnStart(ByVal args() As String)

Dim thWorker As New Thread(AddressOf WorkProcess)

LogFile(Now & ": " & vbTab & "Service started")
thWorker.Start()

End Sub

Protected Overrides Sub OnStop()
LogFile(Now & ": " & vbTab & "Service stopped")
End Sub
Sub WorkProcess()

Do
Thread.Sleep(60000)
If Minute(Now) = 44 Then ExecuteWOL()
Loop

End Sub
....


Thx again.


Regards /Snedker
 
C

Chris Dunaway

Izzy said:
There needs to be a thWorker.Abort() in your OnStop() method.

You should not abort a thread in this manner. You should send some
signal into the thread so it can shutdown in an orderly fashion.
 
I

Izzy

Chris said:
You should not abort a thread in this manner. You should send some
signal into the thread so it can shutdown in an orderly fashion.

Can you give a code example of how to properly shut down a thread?

Izzy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top