Timer Tick Event not Working in Windows Service

G

Guest

I have a Windows Service with a timer but the .Tick event is not being fired/called.

Don't know what code to include...I enabled and started the timer...I have the exact same code in a Windows form and it works fine, but in the service: nothing.

....
this.components = new System.ComponentModel.Container();
this.tmrTimer = new System.Windows.Forms.Timer(this.components);
//
// tmrTimer
//
this.tmrTimer.Interval = 6;
this.tmrTimer.Tick += new System.EventHandler(this.tmrTimer_Tick);

....

//In OnStart or Initialization -- tried both...
tmrTimer.Start();
tmrTimer.Enabled = true;
/*
also tried
tmrTimer.Enabled = true;
tmrTimer.Start();
*/
....
private void tmrTimer_Tick(object sender, System.EventArgs e) {
tCurrTime = DateTime.Now;
EventLog.WriteEntry("Timer Tick", "We have ticked...",
EventLogEntryType.Error);

if (tCurrTime.Hour == 0){
DoMidNightService();
}




Even a "did you check this" would be a great help.

Thanks,
J.
 
K

Ken Kolda

Use either the Timer class from either System.Threading or System.Timers
instead.

Ken


J. Hill said:
I have a Windows Service with a timer but the .Tick event is not being fired/called.

Don't know what code to include...I enabled and started the timer...I have
the exact same code in a Windows form and it works fine, but in the service:
nothing.
 
J

Jay B. Harlow [MVP - Outlook]

J.
In addition to the other comments.

In addition to the others comments, the following articles in MSDN Magazine
explain the difference between the three timer objects in .NET & when to use
each.

http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx

http://msdn.microsoft.com/msdnmag/issues/04/03/default.aspx

The above articles also discusses if & how each timer interacts with
threading.

I'm using System.Timers.Timer (instead of System.Threading.Timer) in my
Windows Service, as I don't really need the extra features of
System.Threading.Timer. Either should work in your case.

Hope this helps
Jay


J. Hill said:
I have a Windows Service with a timer but the .Tick event is not being fired/called.

Don't know what code to include...I enabled and started the timer...I have
the exact same code in a Windows form and it works fine, but in the service:
nothing.
 
A

Adlai Stevenson

J. Hill said:
EXCELLENT!!!
I had just 'migrated' my code from my development Windows app and brought the timer code over. As soon as I created a 'new' timer of System.Timers.Timer it worked perfectly.

Thanks a ton!!!

I do have another question, do I mark this thread "answered" so in case anyone else is as boneheaded as me, they will be able to know the solution is here?

This is Usenet.

Not RAID.
 
L

Laura T.

I use System.Timers.Timer in all my service programs and they work fine.
The Timer.Elapsed event will get thru regulary.
 
G

Guest

I have written a function in my windows service. on my service starts the
timer is started and after some interval i stopped the timer and calling my
function. but my function is not working.

can any one tell me how to call my function in an interval of period.

i want the timer should start first and some interval of time the timers
wants to stop at that time my function should execute. after my function
excution the timer should start. again some interval the timer wants to stop
and my function should execute.

can any one help me.

Regards
William
 

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