2016-03-29 15 views
-1

私はASP.NET5アプリケーションを開発しており、特定の遅延の後にサーバー上でイベントをトリガーしたいと思っています。私はまた、クライアントがイベントの実行をキャンセルするためにサーバーに要求を送信できるようにしたい。 Timerを保存するにはどうすればいいですか。Change(Timeout.Infinite, Timeout.Infinite)に電話して別のリクエストでキャンセルできますか?特定の遅延の後にサーバー上でイベントを発生させる

public class ApiController : Controller 
{ 
    public IActionResult SetTimer() 
    { 
     TimerCallback callback = new TimerCallback(EventToRaise); 
     Timer t = new Timer(callback, null, 10000, Timeout.Infinite); 
     //I need to persist Timer instance somehow in order to cancel the event later 
     return HttpOkObjectResult(timerId); 
    } 

    public IActionResult CancelTimer(int timerId) 
    { 
     /* 
     here I want to get the timer instance 
     and call Change(Timeout.Infinite, Timeout.Infinite) 
     in order to cancel the event 
     */ 
     return HttpOkResult(); 
    } 

    private void EventToRaise(object obj) 
    { 
     ///.. 
    } 
} 
私は EventToRaiseの実行を遅らせるためにSystem.Threading.Timerを使用しています

、私のアプローチは正しいですか、私はそれにいくつかの他の方法を行う必要があります ?それを達成する最良の方法は何ですか?

+1

=> http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx – CodeNotFound

+0

@CodeNotFoundこれを読みます、ありがとう、私は記事で推奨されるように私はHangfireを使用すると思います。 – koryakinp

答えて

0

Quartz.NETは以下のように使用できます。一方、IISベースのトリガーの問題については、Quartz.net scheduler doesn't fire jobs/triggers once deployedで私の答えを見てください。

のGlobal.asax:

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


EmailJob.cs:

using Quartz; 

public class EmailJob : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     SendEmail(); 
    } 
} 


JobScheduler.cs:

using Quartz; 
using Quartz.Impl; 

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

     IJobDetail job = JobBuilder.Create<EmailJob>().Build(); 
     ITrigger trigger = TriggerBuilder.Create() 
      .WithIdentity("trigger1", "group1") 
      //.StartAt(new DateTime(2015, 12, 21, 17, 19, 0, 0)) 
      .StartNow() 
      .WithSchedule(CronScheduleBuilder 
       .WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday, 10, 00) 
       //.WithMisfireHandlingInstructionDoNothing() //Do not fire if the firing is missed 
       .WithMisfireHandlingInstructionFireAndProceed() //MISFIRE_INSTRUCTION_FIRE_NOW 
       .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("GTB Standard Time")) //(GMT+02:00) 
       ) 
      .Build(); 
     scheduler.ScheduleJob(job, trigger); 
    } 
} 

・ホープ、このことができます...

関連する問題