Need help on windows service and timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lemune

    #1

    Need help on windows service and timer

    Hello everyone.
    I'm using vb 2005. I'm creating program that run as service on windows.
    And in my program I need to use timer, so I'm using timer object from
    component. I try my source code on another project that use windows
    form and it work. But when I implement my source code on my program
    that run as service on windows (I have change the code so it would work
    on service but I still use timer object from component), and it doesn't
    work. I'm suspecting that the problem is because I use timer object
    from component. I need some suggestion here. Thanks in advances.

  • Phill W.

    #2
    Re: Need help on windows service and timer

    Lemune wrote:
    I'm using vb 2005. I'm creating program that run as service on windows.
    And in my program I need to use timer, so I'm using timer object from
    component.
    System.Threadin g.Timer?
    System.Timers.T imer?
    System.Windows. Forms.Timer?
    But when I implement my source code on my program that run as service
    on windows (I have change the code so it would work on service but I
    still use timer object from component), and it doesn't work.
    As ever, /please/ define "doesn't work".

    The Service doesn't start [properly]?
    The Timer never seems to fire?
    The Timer fires initially, then "gives up" after a while?
    I'm suspecting that the problem is because I use timer object
    from component.
    System.Timers.T imer is the one you want.

    Better still, only use this Timer to "side-step" the OnStart method's
    call/return sequence (allowing Windows to tell that your service has
    started properly), then kick off your main processing loop and, within
    that, use System.Threadin g.Thread.Sleep( ) /instead/ of a Timer,
    something like:

    Imports System.Threadin g

    Private bRunService as Boolean = True
    Private bStopped as Boolean = False

    Sub OnStart()
    tmrStarter.Star t()
    End Sub

    Sub OnStop()
    ' If you have a long-running process that /must/ finish cleanly,
    ' don't allow OnStop to return until you've cleaned up.
    ' Windows will kill your Service process as soon as OnStop returns.
    bRunService = False
    Do While Not bStopped
    Thread.Sleep( 1000 )
    Loop
    End Sub

    Sub tmrStarter_Elap sed( ... ) _
    Handles tmrStarter.Elap sed

    tmrStarter.Stop ()
    Me.Run()
    End Sub

    Sub Run()
    Do While bRunService
    DoSomethingUsef ul()
    Thread.Sleep( 100 )
    Loop
    bStopped = True
    End Sub

    HTH,
    Phill W.

    Comment

    • Lemune

      #3
      Re: Need help on windows service and timer

      Thank you for your advice.
      >System.Threadi ng.Timer?
      >System.Timers. Timer?
      >System.Windows .Forms.Timer?
      I think I use System.Windows. Forms.Timer. :))
      The Timer never seems to fire?
      My service start just fine, but it seem that the timer never fire.

      I will try using System.Threadin g.Timer. And I will inform you again.

      Comment

      • Lemune

        #4
        Re: Need help on windows service and timer

        Thank you Phill W.
        I have use your suggestion. And my service work. Know i use
        System.Timers.T imer and System.Threadin g.
        Once again thanks Phill

        Comment

        • Lemune

          #5
          Re: Need help on windows service and timer

          Thank you Phill W.
          I have use your suggestion. And my service work. Now i use
          System.Timers.T imer and System.Threadin g.
          Once again thanks Phill

          Comment

          Working...