timer based windows service

experts, what is the best way to create a timer based c# windows service that can run on a specified time of a day once daily.I need assistance in the scheduling part
DevildibAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
probably you can have a deeper research by using Task Scheduler instead? in which it has the flexibility for you to choose its frequency accordingly.
0
Ryan ChongCommented:
a quick ref as a start:

How to Start the Task Scheduler & Use the Basic Task Wizard
http://www.7tutorials.com/how-create-task-basic-task-wizard
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
question: why reinvent the wheel, and just create a simple (console) application, and use the windows scheduled instead ?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

DevildibAuthor Commented:
It's regulatory reqmnt that we can't use scheduled tasks provided by windows scheduler.
0
DevildibAuthor Commented:
It's regulatory reqmnt that we can't use scheduled tasks provided by windows scheduler.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
wondering why, as that one is a supported scheduling tool, which creating "your own" cannot be "certified" better than the windows scheduler???
0
DevildibAuthor Commented:
timer based services has been the trend in our projects
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then why not reuse what you have in the other projects?

anyhow, I would create a timer in the service to run every minute, and check the time the service should actually run (next)

on starting of the service, calculate the "next run time" value, and start the timer
the timer to compare "now" with that value, and if now > that value, run the code and recompute the next "next run time" value
0
DevildibAuthor Commented:
the timer elapsed event to be invoked on a particular time interval, does it implicitly call any thread?are there any issues of multiple instances of the event getting invoked when any error occurs?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, if the service should only run once per day, unless the process takes really long, this would not be needed

nevertheless, yes, you can protect by having a global boolean variable that indicates if the process is actually running

The OnTimer pseudo code would show what I mean:
if !IsRunning
  if Now <= TimeToRun
     IsRunning = true
     dothework
     IsRunning = false
      TimeToRun = CalculateNextTimeToRun
  end if
end if 

Open in new window


of course, you need error handling etc ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.