Windows Service with Timer

G

Guest

Hi all!

I have a Windows service, where I want to use a timer.. But it does not
Seems work?? It does not catch the Timer1.tick event ??? But the code works
on a form???
Can anyone help me, please??

"Onstart" - I set:
Timer1.Interval = 5000
Timer1.Enabled = True
Timer1.Start()

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim MyLog As New EventLog ' create a new event log
' Check if the the Event Log Exists
If Not MyLog.SourceExists("Service") Then
MyLog.CreateEventSource("Service", "Service Log") ' Create Log
End If
MyLog.Source = "Service"

' Write to the Log
MyLog.WriteEntry("Service Log", "This is log on " & _
CStr(TimeOfDay), EventLogEntryType.Information)

End Sub
 
C

Chris Dunaway

Hi all!

I have a Windows service, where I want to use a timer.. But it does not
Seems work?? It does not catch the Timer1.tick event ??? But the code works

Which timer are you using? There are several timers available. If you're
using the one from the System.Windows.Forms.Timer, then it probably won't
work in a service unless you allow the service to interact with the
desktop. Try one of the other timers instead.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
G

Guest

Thanks Chris,
That worked :blush:)

My timer was a system.windows.form.timer??? hmm
Where can I set the "allow the service to interact with the
desktop."

But anyhow,, it worked when i've used the System.timers.timer,, se link

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemtimerstimerclasstopic.asp



Chris Dunaway" <"dunawayc[[at]_lunchmeat said:
Hi all!

I have a Windows service, where I want to use a timer.. But it does not
Seems work?? It does not catch the Timer1.tick event ??? But the code works

Which timer are you using? There are several timers available. If you're
using the one from the System.Windows.Forms.Timer, then it probably won't
work in a service unless you allow the service to interact with the
desktop. Try one of the other timers instead.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

Chris Dunaway

Where can I set the "allow the service to interact with the
desktop."

I'm not sure how to set it programatically, but you can right click on the
service in the Services applet in the control panel and set it there.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
G

Guest

There is a bug in VS.2005. The toolbox for windows services creates a
Windows.Forms.Timer instead of a System.Threading.Timer.

So create your timers programmatically.

Here is a code snippet that works:

Imports System.Threading
Dim myTimer As System.Threading.Timer

In OnStart:
'start the system.threading.timer - its callback runs in a background thread
Dim myTmrCbck As New TimerCallback(AddressOf Timer_Elapsed)
myTimer = New System.Threading.Timer(myTmrCbck, Nothing, 1000, 1000)

in Time_Elapsed:
Private Sub Timer_Elapsed(ByVal state As Object)
Call Beep()
EventLog1.WriteEntry("Timer1 elapsed.")
End Sub

In OnStop:
myTimer.Dispose() 'stop timer

hth herbert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top