2011-09-12 17 views
104

現在、私はC#WPFプロジェクトを作成中です。ユーザーがWindowsタスクスケジューラにスケジュールされたタスクを作成して追加できるようにする必要があります。スケジュールされたタスクの作成

私はこれをやり遂げることができますが、インターネットを検索する際にはあまり見つけ出していないので、指示と参照を使用すると何が必要なのでしょうか。

+1

必要なものはすべてここにあります:http://msdn.microsoft.com/en-us/library/aa383614(v=vs.85).aspx API、サンプル、プログラムで必要なものを達成するための説明。 – kroonwijk

答えて

171

あなたはTask Scheduler Managed Wrapperを使用することができます。

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"); 
     } 
    } 
} 

また、あなたがnative APIを使用するか、Quartz.NETのために行くことができます。詳細については、thisを参照してください。私 https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/

それがうまく流暢APIを設計しているため

+2

はい、Microsoft.Win32.TaskScheduler.dllをダウンロードして参照する必要があります。リンクは答えにあります。 – Dmitry

+0

申し訳ありませんが、私は参照を追加したと思ったが、何らかの理由でそれがそうではなかった。申し訳ありませんが、素晴らしい仕事です。あなたの助けをありがとう – Boardy

+1

@ドミトリーどのようにタスクを開始するのですか? Windowsのスケジューラなどに登録する必要がありますか? – Haroon

17

これは動作します。

//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(); 
+1

この情報をSQLサーバーデータベースに保存する方法はありますか? – Fearcoder

関連する問題