영어로 읽기

다음을 통해 공유


TimerCallback 대리자

정의

Timer의 호출을 처리하는 메서드를 나타냅니다.

[System.Runtime.InteropServices.ComVisible(true)]
public delegate void TimerCallback(object state);
public delegate void TimerCallback(object? state);
public delegate void TimerCallback(object state);

매개 변수

state
Object

이 대리자에서 호출한 메서드와 관련된 애플리케이션 관련 정보를 포함하는 개체 또는 null입니다.

특성

예제

다음 코드 예제에서는 클래스와 함께 사용 하는 대리자를 만드는 방법을 보여 있습니다 Timer .

using System;
using System.Threading;

class TimerExample
{
    static void Main()
    {
        // Create an AutoResetEvent to signal the timeout threshold in the
        // timer callback has been reached.
        var autoEvent = new AutoResetEvent(false);
        
        var statusChecker = new StatusChecker(10);

        // Create a timer that invokes CheckStatus after one second, 
        // and every 1/4 second thereafter.
        Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", 
                          DateTime.Now);
        var stateTimer = new Timer(statusChecker.CheckStatus, 
                                   autoEvent, 1000, 250);

        // When autoEvent signals, change the period to every half second.
        autoEvent.WaitOne();
        stateTimer.Change(0, 500);
        Console.WriteLine("\nChanging period to .5 seconds.\n");

        // When autoEvent signals the second time, dispose of the timer.
        autoEvent.WaitOne();
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
    }
}

class StatusChecker
{
    private int invokeCount;
    private int  maxCount;

    public StatusChecker(int count)
    {
        invokeCount  = 0;
        maxCount = count;
    }

    // This method is called by the timer delegate.
    public void CheckStatus(Object stateInfo)
    {
        AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
        Console.WriteLine("{0} Checking status {1,2}.", 
            DateTime.Now.ToString("h:mm:ss.fff"), 
            (++invokeCount).ToString());

        if(invokeCount == maxCount)
        {
            // Reset the counter and signal the waiting thread.
            invokeCount = 0;
            autoEvent.Set();
        }
    }
}
// The example displays output like the following:
//       11:59:54.202 Creating timer.
//       
//       11:59:55.217 Checking status  1.
//       11:59:55.466 Checking status  2.
//       11:59:55.716 Checking status  3.
//       11:59:55.968 Checking status  4.
//       11:59:56.218 Checking status  5.
//       11:59:56.470 Checking status  6.
//       11:59:56.722 Checking status  7.
//       11:59:56.972 Checking status  8.
//       11:59:57.223 Checking status  9.
//       11:59:57.473 Checking status 10.
//       
//       Changing period to .5 seconds.
//       
//       11:59:57.474 Checking status  1.
//       11:59:57.976 Checking status  2.
//       11:59:58.476 Checking status  3.
//       11:59:58.977 Checking status  4.
//       11:59:59.477 Checking status  5.
//       11:59:59.977 Checking status  6.
//       12:00:00.478 Checking status  7.
//       12:00:00.980 Checking status  8.
//       12:00:01.481 Checking status  9.
//       12:00:01.981 Checking status 10.
//       
//       Destroying timer.

설명

대리자를 사용하여 에서 TimerCallback 호출되는 메서드를 지정합니다 Timer. 이 메서드는 타이머를 만든 스레드에서 실행되지 않습니다. 시스템에서 제공하는 별도의 스레드 풀 스레드에서 실행됩니다. 대리자는 TimerCallback 시작 시간이 경과한 후 메서드를 한 번 호출하고 메서드가 호출될 때까지 또는 메서드가 간격 값 Infinite으로 호출될 때까지 DisposeTimer.Change 타이머 간격당 한 번 호출을 계속합니다.

참고

콜백은 메서드 오버로드가 Dispose() 호출된 후에 발생할 수 있습니다. 타이머는 스레드 풀 스레드에서 실행하기 위해 콜백을 큐에 대기하기 때문입니다. 메서드 오버로드를 Dispose(WaitHandle) 사용하여 모든 콜백이 완료될 때까지 기다릴 수 있습니다.

타이머 대리자는 타이머가 생성될 때 지정되며 변경할 수 없습니다. 의 Timer 시작 시간은 생성자의 매개 변수 TimerdueTime 전달되고 마침표는 매개 변수에 period 전달됩니다. 대리자를 만들고 사용하는 방법을 보여 주는 예제는 TimerCallback 를 참조하세요 System.Threading.Timer.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상

제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

추가 정보