2017-05-18 1 views
-1

작업 또는 작업 시간을 예약 할 때 정확하게 관련되는 것은 무엇입니까? 관리자가 매일 특정 시간에 실행하려고하는 응용 프로그램이 있지만 응용 프로그램은 사용자 입력에 의존하지만 사용자 기본 설정을 저장하고이를로드하도록 설계되었습니다. 사용자가 단추를 클릭하자마자 작업을 수행합니다. 입력 한 모든 데이터가 유효하다고 가정하여 매일 강제로이 작업을 수행하도록하려면 어떻게해야합니까? 이것은 MVC/ASP.NET에서이므로 Windows에있을 것입니다. 하지만 누군가가 리눅스에서 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 서비스에 대해 읽어야한다고 생각합니다.이 참조 http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/ –

관련 문제