0

I have service. I want to verify, if date now is equal to some date,will do something(will send mail).

How can I use Windows Task Scheduler or other ways for event that fires yearly in one and the same day (November, 15 for example)

Please give me example of using Windows Task Scheduler (classes, arguments, properties, methods) related with some date. Can I use timer?

user2889383
  • 89
  • 1
  • 2
  • 8

2 Answers2

0

I see two options

  1. Windows Task Scheduler with a repeating task that executes your application
  2. Windows Service that polls the date at [some_time] every day.

In the first option, you won't need a timer as windows is managing the clock polling for you. In the second option, a timer is appropriate. Working out how long to make it is up to you!

Edit - some insanely trivial code examples

Option 1. call into the windows task scheduler executable and set up your task using command line -> Schedule task in Windows Task Scheduler C#

Type schtasks /CREATE /? at a command prompt to get you started!

Option 2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeToTarget
{
    class Program
    {
        static TimeSpan m_TimeToTarget = default(TimeSpan);
        static void Main(string[] args)
        {
            //maybe you can load this from file or take it from the commandline as commented below
            //string cmd = Environment.GetCommandLineArgs();

            DateTime now = DateTime.Now;
            DateTime target = new DateTime(now.Year, 11, 15); //November 15th. Use todays year.


            //we want to set a timer for the next occurance of Nov 15th.
            if (DateTime.Now > target)
            {
                //increment the year because today is after Nov 15th but before new year.
                target = new DateTime(now.Year, 11, 15).AddYears(1);
            }

            m_TimeToTarget = target.Subtract(now);

            //kick off a timer to wait for the event
            SetTimer(m_TimeToTarget);

            Console.ReadLine(); //console needs to continue running to keep the process alive as timer runs in background thread
        }

        private static void SetTimer(TimeSpan timeToTarget)
        {
            System.Threading.Timer t = new System.Threading.Timer(DoWork, null, Convert.ToInt32(timeToTarget.TotalMilliseconds), 
                System.Threading.Timeout.Infinite); //dont repeat, we want the time to end when it start our work
        }

        private static void DoWork(object state)
        {
            //Do you work here!

            //restart the time
            SetTimer(m_TimeToTarget);
        }
    }
}
Community
  • 1
  • 1
Gusdor
  • 14,001
  • 2
  • 52
  • 64
0

You have few options:

  • You can schedule your event daily, but inside event handler simply check current date. If current date looks good (like November 15th), then proceed to the useful task.

  • You can schedule few one time events few years ahead.

  • You can create schedule that fires on certain specific event (task scheduler has such option). Then something external will have to log such specific event on Nov 15th.

  • Create your own Windows service which always run and will decide when it is time to start your task.

mvp
  • 111,019
  • 13
  • 122
  • 148