3

I'm trying to start a windows service in task scheduler to run twice a day, one at 12.00pm and next one at 10.00pm on the same day. I want this to run every day. Is this can be done? Thanks a lot in advance

Jon Grah
  • 296
  • 2
  • 13
kas_miyulu
  • 335
  • 1
  • 5
  • 27

3 Answers3

3

You would need to create 2 separate tasks.

To make this go a little faster, you can create the first task to run at 12:00pm. Then just export the task and immediately import the same task under a slightly different name. Edit the imported task with the new time of 10:00pm

export and reimport task xml

And unless you need it to be exactly at 12:00 you should edit trigger to randomize the time by checking the appropriate box. Make the delay time as long as you need.

randomize task start time

Community
  • 1
  • 1
Jon Grah
  • 296
  • 2
  • 13
3

I'm not sure if this is a new feature since the original answers, as this is the first time I wanted to do this, but I found this answer before I noticed that you can set multiple triggers for any task.

So, set up your task for the first time you want it to run, then go to the trigger tab for that task, click new, and set up the second time that you want the task to run.

This is neater than having multiple tasks.

enter image description here

Robert Brooker
  • 2,148
  • 24
  • 22
0
public void RunOnTimer()
    {
        string timeToRunServ = "15:30"; //Time to run the report
        var timeArray = timeToRunServ.Split(';');
        CultureInfo provider = CultureInfo.InvariantCulture;

        foreach (var strTime in timeArray)
        {
            //foreach times in the timeArray, add the times into a TimeSpan list
            timeToRun.Add(TimeSpan.ParseExact(strTime, "g", provider));
        }

        timer = new Timer(50000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        ResetTimer();
    }


    void ResetTimer()
    {
        Logs.WriteLog("Reset Timer called");
        TimeSpan currentTime = DateTime.Now.TimeOfDay;
        TimeSpan? nextRunTime = null;

        //foreach time in the timeToRun list
        foreach (TimeSpan runTime in timeToRun)
        {

            if (currentTime < runTime)
            {
                //If the current time is less than the run time(time has not passed), assgin the run time as the next run time
                nextRunTime = runTime;
                break;
            }
        }
        if (!nextRunTime.HasValue)
        {

            nextRunTime = timeToRun[0].Add(new TimeSpan(24, 0, 0));
        }
        timer.Interval = (nextRunTime.Value - currentTime).TotalMilliseconds;

        string interval = timer.Interval.ToString();

        Logs.WriteLog("timer interval is set to " + interval);

        timer.Enabled = true;
    }


    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        string day = DateTime.Today.DayOfWeek.ToString();

        timer.Enabled = false;
        Logs.WriteLog("Timer elapsed");

        StartProcess() // Call the task you want to perform here
        ResetTimer();
    }
kas_miyulu
  • 335
  • 1
  • 5
  • 27
  • 2
    Perhaps you could add an explanation (in the answer, not in comments) of what the code does and that it is alternative solution to the other answer. Generally, **code only** answers are not considered good answers. – Tom Brunberg Jan 23 '18 at 19:15