Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a timer in C# service 1

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
0
0
ZA
hi everyone,

I have got a service i have developed in C#, on that service i have added some function and they are working fine.
My problem is i want to add a timer on the service. i can add it but when i run my service the timer is not getting focus, the tick event is not triggerd,
does anyone have idea why this is so.
 
This may be a dumb question but did you call the start method?
 
I use timers in my services and i haven't had a problem with them.
Code:
public partial class MySerivce : ServiceBase
{
   private readonly timer;
   public MyService(Timer timer)
   {
       InitializeComponents();
       container.Add(timer);

       this.timer = timer;
       this.timer.Elaspsed += timer_Elasped;
   }

   public override void Start(string[] args)
   {
       timer.Start();
   }

   public override void Stop()
   {
       timer.Stop();
   }

   public override void Dispose()
   {
       timer.Elasped -= timer_Elasped;
   }

   private void timer_Elasped(object sender, ElaspedEventArgs e)
   {
      timer.Stop();
      //do work
      timer.Start();
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you guys for your contributions, i used the threads and it worked.
By i still dont understand why my normal timer didnt tick, my code was a bit different from jmeckley's though.
 
Like Christiaan says -- always use the timer from the System.Threading namespace. The others require a Windows event pump being created behind the scenes in order to work correctly.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top