2013-04-13 2 views
0

안녕하세요 새로운 시작 타이머 중 하나의 시작을 2 초 오프셋으로 지연 시키시겠습니까?은 내가 어떻게 할 수있는, 사이트에

static void Main(string[] args) 
    { 

     Timer aTimer = new Timer(); 
     Timer bTimer = new Timer(); 

     // Hook up the Elapsed event for the timer. 
     aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
     bTimer.Elapsed += new ElapsedEventHandler(OnTimedEventb); 

     // Set the Interval to 4 seconds 
     aTimer.Interval = 4000; 
     aTimer.Enabled = true; 
     bTimer.Interval = 4000; 
     bTimer.Enabled = true; 


     Console.WriteLine("Press the Enter key to exit the program."); 
     Console.ReadLine(); 
    } 
    // Specify what you want to happen when the Elapsed event is 
    // raised. 
    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     Console.WriteLine("The status is on {0}", e.SignalTime); 
    } 
    private static void OnTimedEventb(object source, ElapsedEventArgs b) 
    { 
     Console.WriteLine("The state is off {0}", b.SignalTime); 
    } 

그래서 나는 기본적으로 프로그램이 시작될 때 ON 이벤트가 다음 이초 이후 2012 콘솔 응용 프로그램 대를 사용하여 OFF 이벤트 화재 등

에가하지만 윈도우 형태로 사용하는 것, 일하고자하는 프로그램

+0

2000ms 간격의 타이머가 하나만 있고 ON과 OFF를 번갈아 사용할 수 있습니까? –

+0

그래, 당연히, 나는 단지 ON과 OFF를 구별하는 방법을 확실히 알지 못했다. –

+1

On과 Off를 위해서, 그냥 부울 필드가있다; on은 true이고 off는 false이며 타이머 이벤트가 끝나면 뒤집습니다. –

답변

0

예를 들어, IsOn이라는 클래스 수준의 bool을 만들고이를 토글 할 수 있습니다. true은 켜져 있고, false은 꺼져 있음을 의미하므로 한 타이머 만 필요합니다.

private static bool IsOn = true; //default to true (is on) 
static void Main(string[] args) 
{ 

    Timer aTimer = new Timer(); 


    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);   

    // Set the Interval to 2 seconds 
    aTimer.Interval = 2000; 
    aTimer.Enabled = true;  


    Console.WriteLine("Press the Enter key to exit the program."); 
    Console.ReadLine(); 
} 


// Specify what you want to happen when the Elapsed event is 
// raised. 
private static void OnTimedEvent(object source, ElapsedEventArgs e) 
{ 
    IsOn = !IsOn; 
    Console.WriteLine("The status is {0} {1}", IsOn.ToString(), e.SignalTime); 
} 
+0

감사합니다. 나는 클래스 수준에서 bool을 선언하려고 노력하고 있었다. 그리고 나는 단지 그것을 잘못하고 있었다라고 생각한다? 몰라,하지만 고마워 !! –

+0

반갑습니다. 클래스 수준은 맞지만 어쩌면 정적으로 만들지 않았으므로 정적 메서드 내에서 액세스 할 수 없었습니다. – keyboardP

+0

와우, 예, 그 의미가 맞습니다. 다시 한 번 감사드립니다! –