2017-07-14 2 views
0

私は、継続的に稼働し続けるスケジューラを作成する方法を提案しています。現在の時間に基づいてロジックを実行します。UNIXのcronジョブのようなものです。実行し続けるcronジョブスケジューラのようなものを作成する方法は?

私はGOOGLE @https://www.experts-exchange.com/questions/23481296/how-to-create-a-job-schedule-using-C-in-a-console-application.htmlの投稿を見ましたが、これは私の要件を満たしていないと思いますか?

using System; 
using System.Threading; 

public class ThreadWork 
{ 
    public static void DoWork() 
    { 
while(true) 
      { 
       if (DateTime.Now.Hour == 17&DateTime.Now.Minute==0&DateTime.Now.Second==0) 
       { 
        myMethod(); 
       Thread.Sleep(1000*60); 
       } 
       Thread.Sleep(1000*60); 
      } 
    } 
} 
class ThreadTest 
{ 
    public static void Main() 
    { 
     ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); 
     Thread myThread = new Thread(myThreadDelegate); 
     myThread.Start(); 
    } 
} 
+0

https://www.codeproject.com/Articles/591271/A-Simple-Scheduler-in-Csharp – mybirthname

+2

必要な機能を作成し、Windowsタスクスケジューラを使用して特定の時間に実行するのはなぜですか? – logix

+0

これをWebアプリケーション内で実行する必要がある場合は、ホイールを再開発せずに[Hangfire.io](https://www.hangfire.io/)や[Quartz.net](https: //www.quartz-scheduler.net/)。 –

答えて

0

私はquartz.netを試すことをお勧め - https://www.quartz-scheduler.net/

あなたは異なる間隔で実行する別のタスクを作成することができます。メインクラスは、次のようなものになり、ジョブスケジューラのようになります。

public class JobScheduler 
{ 
    public static void Start() 
    { 
     IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); 
     scheduler.Start(); 

     IJobDetail emailJob = JobBuilder.Create<EmailJob>().StoreDurably().WithIdentity("massEmail", "emailGroup").Build(); 


     ITrigger trigger = TriggerBuilder.Create() 
          .WithIdentity("massEmailTrigger", "emailGroup") 
          .WithSimpleSchedule(x => x 
           .WithIntervalInMinutes(1) 
           .RepeatForever()) 
          .Build(); 

     scheduler.ScheduleJob(emailJob, trigger); 
    } 
} 

仕事を希望のようなものになります。

public class EmailJob : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     //Execute the task here.... 
    } 
} 

を次に、あなたはこのようなあなたのglobal_asaxのApplication_Startメソッドでそれを起動します:

protected void Application_Start() 
{ 
    JobScheduler.Start(); 
} 

ウェブサイトの例を参照してください...さまざまな時間設定があります。

関連する問題