2014-09-19 156 views
8

我需要在後臺執行任務,所以我的任務是什麼?我有一個表格來存儲每個客戶的租金數額,所以我需要在每個月後計算出特定的租金價格,因此我使用了Google搜索功能,並且找到了一段代碼(它被稱爲webbackgrounder),我添加了它以我的解決方案,這讓我這個部分的代碼來處理我的任務:在MVC中使用webbackgrounder nuget長時間運行後臺任務

using System; 
using System.Threading; 
using System.Threading.Tasks; 

    namespace WebBackgrounder.DemoWeb 
    { 
     public class SampleJob : Job 
     { 
      public SampleJob(TimeSpan interval, TimeSpan timeout) 
       : base("Sample Job", interval, timeout) 
      { 
      } 

      public override Task Execute() 
      { 
       return new Task(() => Thread.Sleep(3000)); 
      } 
     } 
    } 

我想知道我可以計劃我的任務是什麼?

更多細節:Here

我發現this article,其實我不知道我可以用這個方法對於長期? 此致敬禮。

任何想法將不勝感激。

回答

15

您還需要將類添加到應用程序的App_Start文件夾中,該文件夾將啓動作業並管理其生命週期。你可以在這裏看到一個例子... https://github.com/NuGet/WebBackgrounder/tree/master/src/WebBackgrounder.DemoWeb

這裏是演示程序

using System; 
using Elmah; 
using WebBackgrounder.Jobs; 

[assembly: WebActivator.PostApplicationStartMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Shutdown")] 

namespace WebBackgrounder.DemoWeb.App_Start 
{ 
    public static class WebBackgrounderSetup 
    { 
     static readonly JobManager _jobManager = CreateJobWorkersManager(); 

     public static void Start() 
     { 
      _jobManager.Start(); 
     } 

     public static void Shutdown() 
     { 
      _jobManager.Dispose(); 
     } 

     private static JobManager CreateJobWorkersManager() 
     { 
      var jobs = new IJob[] 
      { 
       new SampleJob(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20)), 
       /* new ExceptionJob(TimeSpan.FromSeconds(15)), */ 
       new WorkItemCleanupJob(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5), new WorkItemsContext()) 
      }; 

      var coordinator = new WebFarmJobCoordinator(new EntityWorkItemRepository(() => new WorkItemsContext())); 
      var manager = new JobManager(jobs, coordinator); 
      manager.Fail(ex => Elmah.ErrorLog.GetDefault(null).Log(new Error(ex))); 
      return manager; 
     } 
    } 
} 

代碼但是我發現它簡單到只使用Webbackgrounder的,我需要如下的部件。將這一類的App_Start文件夾

using System; 
using BombaySapphireCds.Jobs; 
using Elmah; 

[assembly: WebActivator.PostApplicationStartMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Shutdown")] 

namespace BombaySapphireCds.App_Start 
{ 
    public static class PodMonitorConfig 
    { 
     private static PodMonitorJob m_job; 

     public static void Start() 
     { 
      m_job = new PodMonitorJob(TimeSpan.FromSeconds(20)); 
     } 

     public static void Shutdown() 
     { 
      m_job.Dispose(); 
     } 
    } 
} 

和類做實際的工作......(把這個您想要的位置)

using System; 
using System.Threading; 
using System.Threading.Tasks; 

namespace BombaySapphireCds.Jobs 
{ 
    public class PodMonitorJob : IDisposable 
    { 
     private CancellationTokenSource m_cancel; 
     private Task m_task; 
     private TimeSpan m_interval; 
     private bool m_running; 

     public PodMonitorJob(TimeSpan interval) 
     { 
      m_interval = interval; 
      m_running = true; 
      m_cancel = new CancellationTokenSource(); 
      m_task = Task.Run(() => TaskLoop(), m_cancel.Token); 
     } 

     private void TaskLoop() 
     { 
      while (m_running) 
      { 
       // 
       // Do monitoring work here. 
       // 

       Thread.Sleep(m_interval); 
      } 
     } 

     public void Dispose() 
     { 
      m_running = false; 

      if (m_cancel != null) 
      { 
       try 
       { 
        m_cancel.Cancel(); 
        m_cancel.Dispose(); 
       } 
       catch 
       { 
       } 
       finally 
       { 
        m_cancel = null; 
       } 
      } 
     } 
    } 
} 
相關問題