2017-05-18 11 views
-1

タスクまたはクロールジョブをスケジュールするときに正確に関係するものは何ですか?私はマネージャーがある時間に毎日実行したいアプリケーションを持っていますが、アプリケーションはユーザーの入力に依存していますが、ユーザーの設定を保存してそれらをロードするように設計されています。入力したすべてのデータが有効であると仮定して、これを毎日強制的に実行する方法についてはどうしたらよいですか。これはMVC/ASP.NETのため、Windows上にあるはずです。しかし、誰かがそれがどのようにLinuxのcronジョブで動作するかを説明できるなら、そこからも分かります。私は私のmvcコードを呼び出すスクリプトを記述する必要がありますか?または任意の提案?毎日のタスク/ cronジョブでプログラムを実行していますか?

+0

その特定のタスクのためのWindowsサービスを作成し、それに応じて –

答えて

0

これは、指定された時間に毎日実行されるサンプルのWindowsサービスです。これが役立つと思います。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Globalization; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading.Tasks; 

namespace DemoWinService 
{ 
    public partial class Service1 : ServiceBase 
    { 
     public Service1() 
     { 
      InitializeComponent(); 
     } 

     System.Timers.Timer _timer; 
     List<TimeSpan> timeToRun = new List<TimeSpan>(); 
     public void OnStart(string[] args) 
     { 

      string timeToRunStr = "19:01;19:02;19:00"; //Time interval on which task will run 
      var timeStrArray = timeToRunStr.Split(';'); 
      CultureInfo provider = CultureInfo.InvariantCulture; 

      foreach (var strTime in timeStrArray) 
      { 
       timeToRun.Add(TimeSpan.ParseExact(strTime, "g", provider)); 
      } 
      _timer = new System.Timers.Timer(60 * 100 * 1000); 
      _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 
      ResetTimer(); 
     } 


     void ResetTimer() 
     { 
      TimeSpan currentTime = DateTime.Now.TimeOfDay; 
      TimeSpan? nextRunTime = null; 
      foreach (TimeSpan runTime in timeToRun) 
      { 

       if (currentTime < runTime) 
       { 
        nextRunTime = runTime; 
        break; 
       } 
      } 
      if (!nextRunTime.HasValue) 
      { 
       nextRunTime = timeToRun[0].Add(new TimeSpan(24, 0, 0)); 
      } 
      _timer.Interval = (nextRunTime.Value - currentTime).TotalMilliseconds; 
      _timer.Enabled = true; 

     } 

     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      _timer.Enabled = false; 
      Console.WriteLine("Hello at " + DateTime.Now.ToString()); //You can perform your task here 
      ResetTimer(); 
     } 
    } 
} 
+0

こんにちは、それを自動化しますが、このサービスが何をするのか説明できますか?私はスケジューリングタスク(ありがとう)のためにそれを得るが、私はいくつかの数字が混乱して使用される?文字列timeToRunStr = "19:01; 19:02; 19:00"; – ynot269

+0

私はあなたがWindowsサービスについて読む必要があると思う、このref http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/に行く –

関連する問題