Windows Service and timers madness

  • Thread starter Cory J. Laidlaw, Beyond01.com
  • Start date
C

Cory J. Laidlaw, Beyond01.com

I have read several articles here and on the web about using forms and
timers, spcifically in Windows Services

I am using Visual Studio 2008 (vb.net 2008). When creating my windows
service, the timer events never fire, so the code never executes.

in the article http://support.microsoft.com/default.aspx/kb/820639 , it says
to be sure to drag the timer component from the COMPONENTS tab instead of the
Windows FORMS tab, which I did. the component still creates as a
system.windows.forms.timer object, just like it does from the windows forms
tab.

In the articles I have read, its also important to use the Timer_Elapsed
event, not the Timer_Tick event. However, in the editor, Elapsed isn't
available at all, only the tick event.

My head is going to explode. Can anyone provide any pearls of wizdom? Thanks
much!!

Cory
 
L

Lloyd Sheen

"Cory J. Laidlaw, Beyond01.com"
I have read several articles here and on the web about using forms and
timers, spcifically in Windows Services

I am using Visual Studio 2008 (vb.net 2008). When creating my windows
service, the timer events never fire, so the code never executes.

in the article http://support.microsoft.com/default.aspx/kb/820639 , it
says
to be sure to drag the timer component from the COMPONENTS tab instead of
the
Windows FORMS tab, which I did. the component still creates as a
system.windows.forms.timer object, just like it does from the windows
forms
tab.

In the articles I have read, its also important to use the Timer_Elapsed
event, not the Timer_Tick event. However, in the editor, Elapsed isn't
available at all, only the tick event.

My head is going to explode. Can anyone provide any pearls of wizdom?
Thanks
much!!

Cory

You are right about the drag/drop of the timer. What I would do is create
the timer in your program.

You will want the System.Timers.Timer, once you have the object use
AddHandler to add handler for the elapsed event. Make sure that the Timer
you create will not go out of scope or will not be collected in the garbage
collection.

Hope this helps
LS
 
F

Family Tree Mike

"Cory J. Laidlaw, Beyond01.com"
I have read several articles here and on the web about using forms and
timers, spcifically in Windows Services

I am using Visual Studio 2008 (vb.net 2008). When creating my windows
service, the timer events never fire, so the code never executes.

in the article http://support.microsoft.com/default.aspx/kb/820639 , it
says
to be sure to drag the timer component from the COMPONENTS tab instead of
the
Windows FORMS tab, which I did. the component still creates as a
system.windows.forms.timer object, just like it does from the windows
forms
tab.

In the articles I have read, its also important to use the Timer_Elapsed
event, not the Timer_Tick event. However, in the editor, Elapsed isn't
available at all, only the tick event.

My head is going to explode. Can anyone provide any pearls of wizdom?
Thanks
much!!

Cory


Probably the System.Timers.Timer is not in the componets list. It is not
there by default. You can add it to the components list if you want by
right clicking in that list and selecting "Choose Toolbox Items". Scroll
down and check the System.Timers.Timer version.

I've seen many times this has tripped up people as the documents say to add
a component that isn't there by default...

It really is easier as Lloyd said and add it in code manually.
 
C

Cory J. Laidlaw, Beyond01.com

thanks Lloyd for the help! Awesome! For the benefit of others, I got it to
work using the following:

Public Class Service1

Dim timer1 As New System.Timers.Timer

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.
AddHandler timer1.Elapsed, AddressOf Timer1_Tick
timer1.Interval = 5000
timer1.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Timer1.Enabled = False
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
System.Diagnostics.EventLog.WriteEntry("Service1", "Tick!")
End Sub

End Class
 
C

Cor Ligthert[MVP]

Cory,

In version 2003 it was as far as I remember me the Windows.Timers.timer, I
assume there is something gone wrong with the one of the latter versions.

I have reported this.

Cor

"Cory J. Laidlaw, Beyond01.com"
 
M

Michel Posseth [MCP]

I always used system.threading.timer in my services wich worked fine for me
, however once we had a discussion here and
Phill Webber brought up an interesting point , wich i tested and confirmed
in one of my projects at that time , since then i use a loop with a thread
sleep


here is Phill`s argument at that time

#####

IMHO, there's one very Good Reason for /not/ using a Timer.

It's called JIT compilation.


You write a Service and put a Timer in it.
You code up the routine that the Timer calls.
This routine references an external Assembly.


You deploy the Service.
Somehow, you miss the dependent assembly.


Your deployed service runs!
It starts and stops perfectly!
It doesn't report /any/ errors or Exceptions!


But it never does any useful work.


Why???


When the Timer fires, the runtime attempts to JIT the method invoked by
the Timer.
With the referenced Assembly /missing/, this JIT-linking fails but the
runtime doesn't report this and, if the Exception gets logged anywhere,
I've /never/ managed to find it. You can't catch this Exception - it's
gets thrown into the depths of the runtime and the Service
Infrastructure - you never see it.


I've found that calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception if you really want to. ;-)


Regards,
Phill W.





######


regards

Michel Posseth




"Cory J. Laidlaw, Beyond01.com"
 
M

Michel Posseth [MCP]

hello Cory

Don`t use a normall timer in a service

use a threading timer or a loop with a thread sleep


HTH

Michel



"Cory J. Laidlaw, Beyond01.com"
 
D

Dinesh Prabakaran

Hallo Cory,

I tried to as the same as yours but at addressof timer1_tick iam getting error that the "the Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) method does not have the same signature like the delegate Delegate sub "Delegate Sub ElapsedEventHandler(sender As Object, e As System.Timers.ElapsedEventArgs)".

The understand the problem that two method are not same but why iam getting this error ?..

should i add some component to the service form or what ?..

Thank you..

Dinesh



CoryJLaidlawBeyond01co wrote:

thanks Lloyd for the help! Awesome!
09-Jan-09

thanks Lloyd for the help! Awesome! For the benefit of others, I got it to
work using the following

Public Class Service

Dim timer1 As New System.Timers.Time

Protected Overrides Sub OnStart(ByVal args() As String
' Add code here to start your service. This method should set thing
' in motion so your service can do its work
AddHandler timer1.Elapsed, AddressOf Timer1_Tic
timer1.Interval = 500
timer1.Enabled = Tru
End Su

Protected Overrides Sub OnStop(
' Add code here to perform any tear-down necessary to stop your
service
Timer1.Enabled = Fals
End Su

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs
System.Diagnostics.EventLog.WriteEntry("Service1", "Tick!"
End Su

End Clas

:

Previous Posts In This Thread:

Windows Service and timers madness
I have read several articles here and on the web about using forms and
timers, spcifically in Windows Service

I am using Visual Studio 2008 (vb.net 2008). When creating my windows
service, the timer events never fire, so the code never executes

in the article http://support.microsoft.com/default.aspx/kb/820639 , it says
to be sure to drag the timer component from the COMPONENTS tab instead of the
Windows FORMS tab, which I did. the component still creates as a
system.windows.forms.timer object, just like it does from the windows forms
tab

In the articles I have read, its also important to use the Timer_Elapsed
event, not the Timer_Tick event. However, in the editor, Elapsed isn't
available at all, only the tick event

My head is going to explode. Can anyone provide any pearls of wizdom? Thanks
much!

Cory

Re: Windows Service and timers madness
"Cory J. Laidlaw, Beyond01.com"

You are right about the drag/drop of the timer. What I would do is create
the timer in your program

You will want the System.Timers.Timer, once you have the object use
AddHandler to add handler for the elapsed event. Make sure that the Timer
you create will not go out of scope or will not be collected in the garbage
collection

Hope this help
LS

Re: Windows Service and timers madness
"Cory J. Laidlaw, Beyond01.com"

Probably the System.Timers.Timer is not in the componets list. It is not
there by default. You can add it to the components list if you want by
right clicking in that list and selecting "Choose Toolbox Items". Scroll
down and check the System.Timers.Timer version

I've seen many times this has tripped up people as the documents say to add
a component that isn't there by default..

It really is easier as Lloyd said and add it in code manually

--
Mike

thanks Lloyd for the help! Awesome!
thanks Lloyd for the help! Awesome! For the benefit of others, I got it to
work using the following

Public Class Service

Dim timer1 As New System.Timers.Time

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.
AddHandler timer1.Elapsed, AddressOf Timer1_Tick
timer1.Interval = 5000
timer1.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Timer1.Enabled = False
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
System.Diagnostics.EventLog.WriteEntry("Service1", "Tick!")
End Sub

End Class

:

Cory,In version 2003 it was as far as I remember me the Windows.Timers.
Cory,

In version 2003 it was as far as I remember me the Windows.Timers.timer, I
assume there is something gone wrong with the one of the latter versions.

I have reported this.

Cor

"Cory J. Laidlaw, Beyond01.com"

I always used system.threading.
I always used system.threading.timer in my services wich worked fine for me
, however once we had a discussion here and
Phill Webber brought up an interesting point , wich i tested and confirmed
in one of my projects at that time , since then i use a loop with a thread
sleep


here is Phill`s argument at that time


IMHO, there's one very Good Reason for /not/ using a Timer.

It's called JIT compilation.


You write a Service and put a Timer in it.
You code up the routine that the Timer calls.
This routine references an external Assembly.


You deploy the Service.
Somehow, you miss the dependent assembly.


Your deployed service runs!
It starts and stops perfectly!
It doesn't report /any/ errors or Exceptions!


But it never does any useful work.


Why???


When the Timer fires, the runtime attempts to JIT the method invoked by
the Timer.
With the referenced Assembly /missing/, this JIT-linking fails but the
runtime doesn't report this and, if the Exception gets logged anywhere,
I've /never/ managed to find it. You can't catch this Exception - it's
gets thrown into the depths of the runtime and the Service
Infrastructure - you never see it.


I've found that calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception if you really want to. ;-)


Regards,
Phill W.







regards

Michel Posseth




"Cory J. Laidlaw, Beyond01.com"
<[email protected]> schreef in bericht

hello CoryDon`t use a normall timer in a serviceuse a threading timer or a
hello Cory

Don`t use a normall timer in a service

use a threading timer or a loop with a thread sleep


HTH

Michel



"Cory J. Laidlaw, Beyond01.com"

System.Timers.Timer
System.Timers.Timer

Using sleep in a Windows Service
I'm using this as my sleep, but when I start my service it returns an error. It actually appears to be working OK, but I am unable to control the service because it is in a perpetual "Starting" state and the Start, Stop options are not available.

System.Threading.Thread.Sleep(600000)

Using sleep in a Windows Service
System.Threading.Thread.Sleep(600000)

I use this in my Windows Service, yet when I Start it it returns an error. It is actually working OK, but I am unable to Start or Stop it as it shows in the Services as perpetually "Starting".


Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorial...f-1716445b26bc/sending-smtp-email-from-w.aspx
 
C

Cor Ligthert[MVP]

I'm not Cory,

But your code shows that you mix up somehow a Windows.Forms.Form.Timer with
a Windows.System.Timers.Timer

Be aware that the sample on MSDN is just a mechanical translated C# sample
(has no events handling)

You can instance the timer WithEvents and then use the handles
YourTimer.Elapsed event in the same way as a windows.Form.Timer.Tick ins
done for you.

Cor



in message
 
M

Michel Posseth [MCP]

Hello Dinesh ,


You would bether not use a timer at all in a windows service , it is much
bether to use a continous loop with a thread sleep
Phill Weber once posted a good explanation regarding this and since then i
made it my coding standard when i write windows services

The reasson is that you are creating a black hole for possible errors when
you use timers in a service , you just one time notice that your service has
stoped functioning
without a tracebale error .

HTH

Michel




"Dinesh Prabakaran" schreef in bericht
 
M

Michel Posseth [MCP]

By the way if you scroll a bit down in this old thread that you have
reposted you will find the complete explanation of Phil
however if you stick with a timer i would recomend the system threading
timer in a windows service .

Wich worked fine for me until i choose to abbandon it due to Phils comments
and after i confirmed Phils findings


HTH

M. Posseth
 

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