타이머

타이머는 특정 시간에 대리자가 호출되도록 지정할 수 있는 간단한 개체입니다. 스레드 풀의 스레드는 대기 작업을 수행합니다.

Timer 클래스를 사용하는 방법은 간단합니다. 콜백 메서드, 콜백에 전달될 상태를 나타내는 개체, 초기 발생 시간, 콜백 호출 사이의 기간을 나타내는 시간에 TimerCallback 대리자를 전달하여 Timer를 만듭니다. 보류 중인 타이머를 취소하려면 Timer.Dispose 함수를 호출합니다.

참고참고

두 개의 다른 Timer 클래스가 있습니다.System.Windows.Forms.Timer 클래스는 비주얼 디자이너와 함께 작동되고 사용자 인터페이스 컨텍스트에서 사용되는 컨트롤이며 사용자 인터페이스 스레드에서 이벤트를 발생시킵니다.System.Timers.Timer 클래스는 Component에서 파생되므로 비주얼 디자이너와 함께 사용할 수 있고, 이벤트를 발생시키기는 하지만 ThreadPool 스레드에서 발생시킵니다.System.Threading.Timer 클래스는 ThreadPool 스레드에서 콜백을 만들고 이벤트 모델은 사용하지 않습니다.또한 이 클래스는 다른 타이머와 달리 콜백 메서드에 상태 개체를 제공하며매우 간단합니다.

다음 코드 예제에서는 Enter 키를 누를 때까지 1초(1000밀리초) 후에 시작하고 매초 작동하는 타이머를 시작합니다. 타이머에 대한 참조가 들어 있는 변수는 클래스 수준 필드로, 타이머가 실행되는 동안 가비지 수집의 영향을 받지 않도록 합니다. 적극적인 가비지 수집에 대한 자세한 내용은 KeepAlive를 참조하십시오.

Imports System
Imports System.Threading

Public Class Example
    Private Shared ticker As Timer

    Public Shared Sub TimerMethod(state As Object)
        Console.Write(".")
    End Sub

    Public Shared Sub Main()
        ticker = New Timer(AddressOf TimerMethod, Nothing, 1000, 1000)

        Console.WriteLine("Press the Enter key to end the program.")
        Console.ReadLine()
    End Sub
End Class
using System;
using System.Threading;

public class Example
{
    private static Timer ticker;

    public static void TimerMethod(object state)
    {
        Console.Write(".");
    }

    public static void Main()
    {
        ticker = new Timer(TimerMethod, null, 1000, 1000);

        Console.WriteLine("Press the Enter key to end the program.");
        Console.ReadLine();
    }
}
using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
    static Timer^ ticker;

public:
    static void TimerMethod(Object^ state)
    {
        Console::Write(".");
    }

    static void Main()
    {
        TimerCallback^ tcb =
           gcnew TimerCallback(&TimerMethod);

        ticker = gcnew Timer(tcb, nullptr, 1000, 1000);

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

int main()
{
    Example::Main();
}

참고 항목

참조

Timer

기타 리소스

스레딩 개체 및 기능