C#의 타이머
Muhammad Maisam Abbas
2024년2월16일
Csharp
Csharp Timer

이 자습서에서는 C#에서 타이머를 만드는 방법에 대해 설명합니다.
C#에서Timer
클래스를 사용하여 타이머 만들기
Timer
클래스는 C#에서 설정된 시간 간격 후에 이벤트를 생성하는 데 사용됩니다. C#의Timer
클래스를 사용하여 지정된 시간 후에 자체적으로 반복되는 이벤트를 생성 할 수 있습니다. Timer.Elapsed
이벤트는 반복 할 이벤트를 지정하는 데 사용됩니다. 다음 코드 예제는 C#의Timer
클래스를 사용하여 지정된 시간 간격 후에 이벤트를 반복하는 타이머를 만드는 방법을 보여줍니다.
using System;
using System.Timers;
namespace timer {
class Program {
private static void OnTimedEvent(object source, ElapsedEventArgs e) {
Console.WriteLine("Hello World!");
}
static void Main(string[] args) {
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 500;
myTimer.Enabled = true;
Console.WriteLine("Press \'e\' to escape the sample.");
while (Console.Read() != 'e')
;
}
}
}
출력:
Press 'e' to escape the sample.
Hello World!
e
위의 코드에서Hello World!
출력을 반복하는 타이머를 만들었습니다. 문자e
가 C#에 입력 될 때까지500
밀리 초마다 먼저Timer
클래스의myTimer
인스턴스를 초기화했습니다. 그런 다음OnTimedEvent()
함수를Time.Elapsed
이벤트에 대한 이벤트로 지정했습니다. 간격은myTimer.Interval
속성을 사용하여500
밀리 초로 설정됩니다. 결국myTimer.Enabled
속성으로 타이머를 시작했습니다.
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn