I'm in the process of writing a windows service using System.Timers.Timer to keep track of my interval. What I'd like to do is make it so my service will launch on a specific day and time based on variables in my app.config file.
2 Answers
I don't think you're really writing a Windows service, I think you're writing a job. A cleaner, and easier approach, to your problem would be to write a Console application and then setup a Windows Scheduled Task to run that Console application at the intervals you want.
Never mind the fact that you'll have to set the time in milliseconds with the Timer
approach, the Windows service would not be capable of handling Daylight Savings Time and more.

- 66,820
- 29
- 157
- 232
-
You could set up a Windows Scheduled Task which lets you define arbitrary and very granular schedules (and other triggers). – Dai Mar 14 '13 at 18:58
-
@Dai, that's where my link leads to. I will change the verbiage in the answer to Windows Scheduled Task. I just called it a job. – Mike Perrenoud Mar 14 '13 at 18:59
-
Windows scheduled task is not an option. I have no permissions to touch or interact with the server. – bbcompent1 Mar 14 '13 at 19:01
-
@bbcompent1, then you may need to go with the possible duplicate that was indicated. On the other hand, considering the difficulty of that approach, do your best to work with those that control the server and see if they can assist. I understand having your hands tied surrounding hardware, but usually, even in my massive organization, I can get what I need. – Mike Perrenoud Mar 14 '13 at 19:03
-
Well, I work for a large government agency that will not change its policies for a lowly contractor. – bbcompent1 Mar 14 '13 at 19:27
-
@MichaelPerrenoud: If you have no access to the server, how do you propose getting your service installed on it? – Jim Mischel Mar 14 '13 at 19:37
-
@Jim, just as I suspected when writing my response he works for a large government agency. I too work in a very similar environment. Though it seems impossible and impenetrable, if you simply provide them the reasons why and the steps to do what you need, you can *generally* get them to do more than you think. However, if you say you can or can't, either way you'll be right, I was trying to give the OP some confidence in their ability to get what they needed. I know I've broken down those walls now more than once and so **it can be done.** – Mike Perrenoud Mar 14 '13 at 19:51
-
@MichaelPerrenoud: My apologies. That comment was supposed to be directed at the OP. But thanks for the explanation. – Jim Mischel Mar 14 '13 at 20:03
-
I can understand where you are coming from. However, the individuals I will provide this installer to are not IT experts, they are normal user body who don't know how to create a scheduled task. I guess I will have to figure this out on my own as I have seen this method in similar windows service programs we have here, I just figured it was worth a try asking someone here. – bbcompent1 Mar 14 '13 at 20:13
-
@bbcompent1 - It would be easier to provide a word document with screenshots and steps of how to set up a scheduled task, than an installer and the logic for a windows service. If you must do a service, you can set a 24 hour timer that starts at the configured time of day, have a small method that checks the day of week, and exit if its not the right day. Could be simple. – Dmitriy Khaykin Mar 14 '13 at 20:24
-
1Or a powershell script / batch file that executes the schtasks command line tool. – Jim Mischel Mar 14 '13 at 20:44
-
Ok, thank you, that is what I was looking for, just the logical flow of how to make it work without being overly complicated. Excellent! – bbcompent1 Mar 15 '13 at 12:08
-
Never mind guys, I got it, I was able to figure out the process. I can post info for anyone here who needs it. – bbcompent1 Apr 13 '13 at 00:55
Although I think your best bet is to make this a scheduled task, you can easily create a waitable timer that will signal at the same time every day. Windows has a Waitable Timer object, but there's no .NET support for it.
I published an article a while back called Waitable Timers in C#, in which I showed how to use this object from a C# program. Unfortunately, the site that published the article is no longer. However, you can download the code examples for the article from my site at http://www.mischel.com/pubs/waitabletimer.zip. You're free to use the code in any way you see fit.

- 131,090
- 20
- 188
- 351
-
-
@Ajay: Unfortunately, the site that published the article no longer exists. I've corrected the reference in the answer. – Jim Mischel Mar 07 '17 at 14:39