每天运行两次任务计划程序,但不是每小时一次

时间:2017-08-25 09:20:51

标签: windows windows-services scheduled-tasks taskscheduler

我正在尝试在任务计划程序中启动Windows服务,每天运行两次,一次在中午12点,下一次在同一天下午10点。我想让它每天都运行。这可以吗?非常感谢提前

3 个答案:

答案 0 :(得分:1)

您需要创建2个单独的任务。

为了加快速度,您可以创建第一个在12:00 pm运行的任务。然后只需export任务,并立即import以略微不同的名称执行相同的任务。使用新时间晚上10:00编辑导入的任务

export and reimport task xml

除非您需要它恰好在12:00,否则您应该通过选中相应的框来编辑触发器以随机化时间。只要你需要,就可以延迟。

randomize task start time

答案 1 :(得分: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();
    }

答案 2 :(得分:0)

我不确定这是否是自原始答案以来的新功能,因为这是我第一次想这样做,但是在我注意到您可以为任何任务设置多个触发器之前我找到了这个答案。< /p>

因此,第一次设置您希望它运行的任务,然后转到该任务的触发器选项卡,单击新建,然后设置您希望该任务第二次运行。

这比有多个任务更整洁。