programing

스케줄링된 작업 만들기

subpage 2023. 6. 18. 16:01
반응형

스케줄링된 작업 만들기

저는 C# WPF 프로젝트를 진행하고 있습니다.사용자가 예약된 작업을 만들고 Windows 작업 스케줄러에 추가할 수 있도록 허용해야 합니다.

인터넷을 검색할 때 많이 찾을 수 없기 때문에 제가 어떻게 이것을 할 수 있고 지침과 참고 자료를 사용하여 무엇이 필요합니까?

작업 스케줄러 관리 래퍼를 사용할 수 있습니다.

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Create a trigger that will fire the task at this time every other day
         td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}

또는 네이티브 API를 사용하거나 Quartz를 사용할 수 있습니다.NET. 자세한 내용은 여기를 참조하십시오.

이것은 저에게 효과가 있습니다. https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/

그것은 훌륭하게 설계된 Fluent API입니다.

//This will create Daily trigger to run every 10 minutes for a duration of 18 hours
SchedulerResponse response = WindowTaskScheduler
    .Configure()
    .CreateTask("TaskName", "C:\\Test.bat")
    .RunDaily()
    .RunEveryXMinutes(10)
    .RunDurationFor(new TimeSpan(18, 0, 0))
    .SetStartDate(new DateTime(2015, 8, 8))
    .SetStartTime(new TimeSpan(8, 0, 0))
    .Execute();

언급URL : https://stackoverflow.com/questions/7394806/creating-scheduled-tasks

반응형