469,946 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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.

Jul 5 '06 #1
4 1756
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.
Jul 5 '06 #2
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.

Jul 6 '06 #3
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

Jul 6 '06 #4
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

Jul 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Henrik H | last post: by
2 posts views Thread by Jesper Stocholm | last post: by
3 posts views Thread by Nathan Kovac | last post: by
2 posts views Thread by linesh.gajera | last post: by
2 posts views Thread by John David Thornton | last post: by
5 posts views Thread by Tom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.