locked
Timer not working in Windows Service RRS feed

  • Question

  • Hi. I'm new in windows service. I did a few test and I could install it. I add a small function in the start event and it run properly. but when I put it in timer event, it didn't run.

    I just add a timer from toolbox and in its event i put the code:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    FileCopy("C:\a.txt", "D:\" & Format(Now, "hh-mm-ss") & ".txt")

    End Sub

    I feel that the timer didn't work... and can I use the windows form timer in a windows service?

    Thanks.

    Omar Mallat

    Friday, October 27, 2006 6:13 PM

Answers

  • i had a similar issue and found that using a system.timers.timer rather than a system.windows.forms.timer sorted it
    Thursday, June 14, 2007 2:12 PM

All replies

  • Hi,

    I guess the timer component relies on some one reading and dispatching windows messages from the message queue like it is down by the implementation of winforms. That is not the case in a service app.

    To do work on a regular interval you could create a new thread which loops until some state tells it to quit. The thread would loop until it should finish and in that loop you would set the thread to sleep a while before doing its work again. Like so:

    Public Sub YourThreadFunction()

      Try

        While True

          DoSomeWork()  ' like your FileCopy operation
          Sleep 10000  ' sleep for 10 seconds

        Wend

      Catch e As ThreadAbortException

        Thread.ResetAbort()

      End Try

    End Sub

    The thread will run until Thread.Abort is called. E.g. in the Stop event of the service.

    --
    SvenC

    Friday, October 27, 2006 8:06 PM
  • i was Tryed using the Thread mechanisum,

    In the Start method of the service i wrote the thread code but when i start the service the serivce not getting stated since the code is in the infinite while loop,

    please suggest on this

    Wednesday, June 6, 2007 9:18 AM
  • as you see, this loop should be in as function called as thread, not a normal function. this appear from its name

    if you just call a function having infinitie loop, you will still in the service start., but if you put your loop in another thread, so the service call the thread and continue the start process.

    if you want to learn about multithreading, just use your MSDN, or try to search here, you will find a lot.

    Thursday, June 7, 2007 2:58 AM
  • i had a similar issue and found that using a system.timers.timer rather than a system.windows.forms.timer sorted it
    Thursday, June 14, 2007 2:12 PM
  •  

    If you are wanting to use a timer within a service the following is the way forward..

    The timer is all code driven and there is no actual control used.

     

    Code Snippet

    Private tmrParse As New System.Timers.Timer(PollTimeMilliSecs)

    ' change PollTimeMilliSecs to the amount of seconds the timer is set for * 1000

    Ie. 5 secs is 5000 millisecs

     

     

    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.

     

    ' this adds the handler for the tick event of the newly created timer.

    AddHandler tmrParse.Elapsed, AddressOf tmrParse_Tick

    tmrParse.Enabled = True

     

    End Sub

     

    Protected Overrides Sub OnStop()

     

    ' Add code here to perform any tear-down necessary to stop your service.

    tmrParse.Enabled = False

    RemoveHandler tmrParse.Elapsed, AddressOf tmrParse_Tick

     

    End Sub

     

    Private Sub tmrParse_Tick(ByVal pSender As Object, ByVal pArgs As System.Timers.ElapsedEventArgs)

     

    'put your code here and it will be run each time the timer event fires

    End Sub

     

     

    tmrParse.enabled = true

     

     

     

    • Proposed as answer by sajitshankar Wednesday, August 26, 2009 6:58 AM
    Friday, August 31, 2007 11:12 AM
  • Dogs_bollox's solution is perfectly working. only use system.timers.timer .............. not system.windows.forms.timer

     

    Thanks ....... dogs_bollox

    Saturday, February 19, 2011 3:09 PM