Home All Groups Group Topic Archive Search About

Need help on windows service and timer

Author
5 Jul 2006 3:14 AM
Lemune
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.

Author
5 Jul 2006 12:14 PM
Phill W.
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.Threading.Timer?
System.Timers.Timer?
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.Timer 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.Threading.Thread.Sleep() /instead/ of a Timer,
something like:

Imports System.Threading

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

Sub OnStart()
    tmrStarter.Start()
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_Elapsed( ... ) _
     Handles tmrStarter.Elapsed

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

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

HTH,
    Phill  W.
Author
6 Jul 2006 2:00 AM
Lemune
Thank you for your advice.

>System.Threading.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.Threading.Timer. And I will inform you again.
Author
6 Jul 2006 3:27 AM
Lemune
Thank you Phill  W.
I have use your suggestion. And my service work. Know i use
System.Timers.Timer and System.Threading.
Once again thanks Phill
Author
6 Jul 2006 3:31 AM
Lemune
Thank you Phill  W.
I have use your suggestion. And my service work. Now i use
System.Timers.Timer and System.Threading.
Once again thanks Phill