Home All Groups Group Topic Archive Search About

Windows Service, timer doesn't tick

Author
25 Sep 2006 12:31 PM
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

Author
25 Sep 2006 1:48 PM
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

Morten Snedker wrote:
Show quoteHide quote
> 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
Author
26 Sep 2006 9:46 AM
Morten Snedker
On 25 Sep 2006 06:48:40 -0700, "Izzy" <israel.rich***@gmail.com>
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
Author
26 Sep 2006 1:50 PM
Izzy
There needs to be a thWorker.Abort() in your OnStop() method.

Izzy


Morten Snedker wrote:
Show quoteHide quote
> On 25 Sep 2006 06:48:40 -0700, "Izzy" <israel.rich***@gmail.com>
> 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
Author
26 Sep 2006 2:58 PM
Chris Dunaway
Izzy wrote:
> 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.
Author
26 Sep 2006 3:03 PM
Izzy
Chris Dunaway wrote:
> Izzy wrote:
> > 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.

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

Izzy
Author
25 Sep 2006 5:54 PM
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
Show quoteHide quote
"Morten Snedker" <morten_spammenot_ATdbconsult.dk> wrote in message
news:g3ifh259cmmuhln3gp0nsanqm20ijoribb@4ax.com...
> 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
Author
25 Sep 2006 9:41 PM
Chris Dunaway
Morten Snedker wrote:
Show quoteHide quote
> 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>