2013-10-21 3 views
0

서비스있어. 나는 지금 날짜가 어떤 날짜와 같으면, 뭔가를 할 것인지를 확인하고 싶다. (메일을 보낼 것이다).1 년에 1 년 1 년에 발생하는 이벤트

내가 Windows 작업 스케줄러 또는 하나에 연간 화재와 같은 날 (예를 들어 11 월 15 일)

Windows 작업 스케줄러 (클래스, 인수를 사용하는 나에게 예를주십시오 이벤트에 대한 다른 방법을 사용할 수있는 방법 속성, 메서드)을 반환합니다. 타이머를 사용할 수 있습니까?

답변

0

나는 응용 프로그램

  • Windows 서비스를 실행하는 반복 작업으로 두 가지 옵션이

    1. Windows 작업 스케줄러를 참조하는 여론 조사 매일 [some_time]의 날짜입니다.

    첫 번째 옵션에서는 Windows에서 시계 폴링을 관리하므로 타이머가 필요하지 않습니다. 두 번째 옵션에서는 타이머가 적절합니다. 그것을 만드는 데 얼마나 오래 일할 것인가는 당신에게 달렸습니다!

    편집-일부 미친 듯이 사소한 코드 예제

    옵션 1. Windows 작업 스케줄러 실행에 호출 및 명령 행 사용하여 작업을 설정할 -에> Schedule task in Windows Task Scheduler C#

    유형 schtasks /CREATE /?을 당신을 시작하게하는 명령 프롬프트!

    옵션 2.

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    
    namespace TimeToTarget 
    { 
        class Program 
        { 
         static TimeSpan m_TimeToTarget = default(TimeSpan); 
         static void Main(string[] args) 
         { 
          //maybe you can load this from file or take it from the commandline as commented below 
          //string cmd = Environment.GetCommandLineArgs(); 
    
          DateTime now = DateTime.Now; 
          DateTime target = new DateTime(now.Year, 11, 15); //November 15th. Use todays year. 
    
    
          //we want to set a timer for the next occurance of Nov 15th. 
          if (DateTime.Now > target) 
          { 
           //increment the year because today is after Nov 15th but before new year. 
           target = new DateTime(now.Year, 11, 15).AddYears(1); 
          } 
    
          m_TimeToTarget = target.Subtract(now); 
    
          //kick off a timer to wait for the event 
          SetTimer(m_TimeToTarget); 
    
          Console.ReadLine(); //console needs to continue running to keep the process alive as timer runs in background thread 
         } 
    
         private static void SetTimer(TimeSpan timeToTarget) 
         { 
          System.Threading.Timer t = new System.Threading.Timer(DoWork, null, Convert.ToInt32(timeToTarget.TotalMilliseconds), 
           System.Threading.Timeout.Infinite); //dont repeat, we want the time to end when it start our work 
         } 
    
         private static void DoWork(object state) 
         { 
          //Do you work here! 
    
          //restart the time 
          SetTimer(m_TimeToTarget); 
         } 
        } 
    } 
    
  • +0

    예 (코드) – user2889383

    +0

    @ user2889383 제 편집을 참조하십시오. – Gusdor

    0

    당신이 가지고있는 몇 가지 옵션 :

    • 당신은 매일 이벤트를 예약하지만, 이벤트 핸들러 내에서 단순히 현재 날짜를 확인할 수 있습니다. 현재 날짜가 좋으면 (11 월 15 일처럼) 유용한 작업으로 진행하십시오.

    • 몇 년 전에 한 번만 이벤트를 예약 할 수 있습니다.

    • 특정 특정 이벤트 (작업 스케줄러에는 이러한 옵션이 있음)에서 실행되는 일정을 만들 수 있습니다. 그러면 외부 이벤트가 11 월 15 일에 특정 이벤트를 기록해야합니다.

    • 항상 실행되는 Windows 서비스를 만들고 작업을 시작할 시간을 결정합니다.

    +0

    예 (코드)를주세요. – user2889383