-2

Writing a C# app to run some scheduled tasks, all I really need is something to continuously monitor the date and time, and call a function once, on the 28th of each month, and then go back to waiting patiently for the 28th of the next month to come around.

I guess it will be using System.Threading.Thread.Sleep() in some way, but as for the specific conditions under which it should run, and how to prevent it from running multiple times on the date in question, I'm not sure...

Any help greatly appreciated :)

JamieS
  • 307
  • 2
  • 13
  • 15
    Wouldn't it be easier to just set it up as a windows scheduled task that only runs on the 28th? – Shawn Steward Oct 26 '12 at 16:07
  • 1
    Quartz.NET 2.0 will handle all of this for you. Bundle into a Windows service and you configure the schedule accordingly? – SpaceBison Oct 26 '12 at 16:08
  • 5
    A console app is definitely not the right tool for this job. – Jesse Carter Oct 26 '12 at 16:08
  • 1
    What's wrong with using Windows Scheduled Tasks, which would automatically run it on the 28th of each month at a specific time, and wouldn't waste the memory or resources keeping the app alive in between? That's exactly what it's designed to be used for; for instance, I have an app scheduled to run at 8:45AM every morning, but only on weekdays. – Ken White Oct 26 '12 at 16:09
  • Check out this article: http://quartznet.sourceforge.net/ – swabs Oct 26 '12 at 16:11
  • @JesseCarter: A console app is perfect for this kind of job, it's just how you go about running it on time that is debatable. My vote is as a scheduled task – musefan Oct 26 '12 at 16:14
  • Do you need to run your task after the 28th if you should miss the schedule due (for example) to the server being down? – SteB Oct 26 '12 at 16:17
  • @musefan I meant in the sense that he is talking about having an always running console application with some kind of thread.sleep() logic to decide if its the right day. Just basing it on what he said in the OP – Jesse Carter Oct 26 '12 at 16:48

2 Answers2

5

You can set up a Windows Scheduled Task to run your console app on the 28th of every month.
It's simple, easy and light on the server's resources since your app wouldn't take up any resources between runs.

If the PC is down, you can also tick the option: "Run task as soon as possible after a scheduled start is missed."

There are a huge number of options for you to customize how your task is run, such as:

  1. Only run when a user is logged on.
  2. Specify program parameters.
  3. Start the task only if the computer is on AC power.
  4. Start the task only if the following network connection is available.

Not all option may be available in all versions of Windows.

SteB
  • 1,999
  • 4
  • 32
  • 57
  • wasn't sure how reliable Windows Task Scheduler would be, especially as this will be running on a desktop PC with downtime, not an always on server. – JamieS Oct 26 '12 at 16:18
  • @JamieS: You can improve reliability by tracking (in a file or DB) the last run time. Then have your program run with windows scheduled tasks and also set it to run on server start up. Then you can also check the last run date and determine if you should run the process or wait for schedule to kick in – musefan Oct 26 '12 at 16:23
  • It's very reliable.. It's also useful to keep application running. You can set up a task to run an application and set the interval to a minute and set it do it doesn't close if an instance is already running. – carny666 Oct 26 '12 at 16:23
  • 2
    @JamieS - Even moreso then, you don't want to have a constantly running program. What if they reboot? What if it's down and misses it's time? WTS handles all that. – Bobson Oct 26 '12 at 16:43
-6

You can use loop which runs each second and checks for the date, and at specified date acts and waits for the next month. You will use System.Threading.Thread.Sleep() to put it on wait for 1 second.

You can wait for the next date by having last date traacked and calculating next date by adding to it 1 monnth.

           DateTime? counter = null;

            while (1 = 1)
            {
                if (DateTime.Now.Day == 28 && (!counter.HasValue || counter.Value.Date < DateTime.Now.Date ))
                {
                    counter = DateTime.Now;

                    DoSomething();
                }

                Thread.Sleep(1000);
            };
Farfarak
  • 1,497
  • 1
  • 8
  • 8
  • 6
    Oh. My. Gawd... I hope you don't get let lose on real programs – musefan Oct 26 '12 at 16:15
  • You do not have timer in Console, and it's answers the question on hand – Farfarak Oct 26 '12 at 16:18
  • 2
    Of course you have Timer in console, you just need to reference the appropriate libraries. Thread sleeping sucks and you will drain your processor with while loops like this – musefan Oct 26 '12 at 16:19
  • 2
    Infinite loop = bad, especially when Task Scheduler will complete the requirements. – Johnie Karr Oct 26 '12 at 16:19
  • 3
    Just for your reference @010001100110000101110010011010. This answer shows exactly how to add a timer to a console app. I don't understand why you've said that you can't. http://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application – zeencat Oct 26 '12 at 16:21
  • 1
    @010001100110000101110010011010 - Delete the answer to recover 12 reputation. – Lion Oct 29 '12 at 17:32