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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
a quick ref as a start:

How to Start the Task Scheduler & Use the Basic Task Wizard
Guy Hengel [angelIII / a3]Billing EngineerCommented:
question: why reinvent the wheel, and just create a simple (console) application, and use the windows scheduled instead ?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

DevildibAuthor Commented:
It's regulatory reqmnt that we can't use scheduled tasks provided by windows scheduler.
DevildibAuthor Commented:
It's regulatory reqmnt that we can't use scheduled tasks provided by windows scheduler.
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???
DevildibAuthor Commented:
timer based services has been the trend in our projects
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
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?
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
     IsRunning = false
      TimeToRun = CalculateNextTimeToRun
  end if
end if 

Open in new window

of course, you need error handling etc ...

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.