Timer 생성자

정의

Timer 클래스의 새 인스턴스를 초기화합니다.

오버로드

Timer(TimerCallback)

새로 만든 Timer 개체를 상태 개체로 사용하고 무한 기간 및 무한 만료 예정 시간을 지정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(TimerCallback, Object, Int32, Int32)

부호 있는 32비트 정수로 시간 간격을 지정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(TimerCallback, Object, Int64, Int64)

부호 있는 64비트 정수로 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(TimerCallback, Object, TimeSpan, TimeSpan)

TimeSpan 값을 사용하여 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(TimerCallback, Object, UInt32, UInt32)

부호 있는 32비트 정수로 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(TimerCallback)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

새로 만든 Timer 개체를 상태 개체로 사용하고 무한 기간 및 무한 만료 예정 시간을 지정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

public:
 Timer(System::Threading::TimerCallback ^ callback);
public Timer (System.Threading.TimerCallback callback);
new System.Threading.Timer : System.Threading.TimerCallback -> System.Threading.Timer
Public Sub New (callback As TimerCallback)

매개 변수

callback
TimerCallback

실행할 메서드를 나타내는 TimerCallback 대리자입니다.

예제

다음 코드 예제에서는 타이머 자체를 상태 개체로 사용하여 새 타이머를 만듭니다. 메서드는 Change 타이머를 시작하는 데 사용됩니다. 타이머 콜백이 발생하면 상태 개체를 사용하여 타이머를 끕니다.

using System;
using System.Threading;

public class Example
{
    public static void Main()
    {
        // Create an instance of the Example class, and start two
        // timers.
        Example ex = new Example();
        ex.StartTimer(2000);
        ex.StartTimer(1000);

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

    public void StartTimer(int dueTime)
    {
        Timer t = new Timer(new TimerCallback(TimerProc));
        t.Change(dueTime, 0);
    }

    private void TimerProc(object state)
    {
        // The state object is the Timer object.
        Timer t = (Timer) state;
        t.Dispose();
        Console.WriteLine("The timer callback executes.");
    }
}
Imports System.Threading

Public Class Example
    Public Shared Sub Main()
        ' Create an instance of the Example class, and start two
        ' timers.
        Dim ex As New Example()
        ex.StartTimer(2000)
        ex.StartTimer(1000)

        Console.WriteLine("Press Enter to end the program.")
        Console.ReadLine()
    End Sub

    Public Sub StartTimer(ByVal dueTime As Integer)
        Dim t As New Timer(AddressOf TimerProc)
        t.Change(dueTime, 0)
    End Sub

    Private Sub TimerProc(ByVal state As Object)
        ' The state object is the Timer object.
        Dim t As Timer = CType(state, Timer)
        t.Dispose()
        Console.WriteLine("The timer callback executes.")
    End Sub
End Class

설명

개체 자체를 상태 개체로 사용 Timer 하려는 경우 이 생성자를 호출합니다. 타이머를 만든 후 메서드를 Change 사용하여 간격 및 기한을 설정합니다.

이 생성자는 개체가 상태 개체에 할당되기 전에 첫 번째 콜백이 발생하지 않도록 첫 번째 콜백 이전의 Timer 무한 기한과 콜백 간의 무한 간격을 지정합니다.

callback 지정된 메서드는 스레드에서 ThreadPool 호출되므로 재진입해야 합니다. 타이머 간격이 메서드를 실행하는 데 필요한 시간보다 작거나 모든 스레드 풀 스레드가 사용 중이고 메서드가 여러 번 큐에 대기 중인 경우 두 스레드 풀 스레드에서 메서드를 동시에 실행할 수 있습니다.

적용 대상

Timer(TimerCallback, Object, Int32, Int32)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

부호 있는 32비트 정수로 시간 간격을 지정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

public:
 Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, int dueTime, int period);
public Timer (System.Threading.TimerCallback callback, object state, int dueTime, int period);
public Timer (System.Threading.TimerCallback callback, object? state, int dueTime, int period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * int * int -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As Integer, period As Integer)

매개 변수

callback
TimerCallback

실행할 메서드를 나타내는 TimerCallback 대리자입니다.

state
Object

콜백 메서드에서 사용할 정보가 포함된 개체를 반환하거나 null을 반환합니다.

dueTime
Int32

callback이 호출되기 전에 지연할 시간(밀리초)입니다. 타이머가 시작되지 않도록 하려면 Infinite를 지정합니다. 타이머를 즉시 시작하려면 0을 지정합니다.

period
Int32

callback 호출 사이의 시간 간격(밀리초)입니다. 정기적으로 신호를 보내지 않도록 하려면 Infinite를 지정합니다.

예외

dueTime 또는 period 매개 변수가 음수이고 Infinite와 같지 않은 경우

callback 매개 변수가 null인 경우

예제

다음 코드 예제에서는 대리자를 TimerCallback 만들고 클래스의 Timer 새 instance 초기화하는 방법을 보여줍니다.

using namespace System;
using namespace System::Threading;

ref class StatusChecker
{
private:
    int invokeCount, maxCount;

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

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

        if (invokeCount == maxCount) {
            // Reset the counter and signal the waiting thread.
            invokeCount  = 0;
            autoEvent->Set();
        }
    }
};

ref class TimerExample
{
public:
    static void Main()
    {
        // Create an AutoResetEvent to signal the timeout threshold in the
        // timer callback has been reached.
        AutoResetEvent^ autoEvent = gcnew AutoResetEvent(false);

        StatusChecker^ statusChecker = gcnew StatusChecker(10);

        // Create a delegate that invokes methods for the timer.
        TimerCallback^ tcb =
           gcnew TimerCallback(statusChecker, &StatusChecker::CheckStatus);

        // 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);
        Timer^ stateTimer = gcnew Timer(tcb, autoEvent, 1000, 250);

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

        // When autoEvent signals the second time, dispose of the timer.
        autoEvent->WaitOne(5000, false);
        stateTimer->~Timer();
        Console::WriteLine("\nDestroying timer.");
    }
};

int main()
{
    TimerExample::Main();
}
// 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.
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.
Imports System.Threading

Public Module Example
    Public Sub Main()
        ' Use an AutoResetEvent to signal the timeout threshold in the
        ' timer callback has been reached.
        Dim autoEvent As New AutoResetEvent(False)

        Dim statusChecker As 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." & vbCrLf, 
                          DateTime.Now)
        Dim stateTimer As New Timer(AddressOf statusChecker.CheckStatus, 
                                    autoEvent, 1000, 250)

        ' When autoEvent signals, change the period to every half second.
        autoEvent.WaitOne()
        stateTimer.Change(0, 500)
        Console.WriteLine(vbCrLf & "Changing period to .5 seconds." & vbCrLf)

        ' When autoEvent signals the second time, dispose of the timer.
        autoEvent.WaitOne()
        stateTimer.Dispose()
        Console.WriteLine(vbCrLf & "Destroying timer.")
    End Sub
End Module

Public Class StatusChecker
    Dim invokeCount, maxCount As Integer 

    Sub New(count As Integer)
        invokeCount  = 0
        maxCount = count
    End Sub

    ' The timer callback method.
    Sub CheckStatus(stateInfo As Object)
        Dim autoEvent As AutoResetEvent = DirectCast(stateInfo, AutoResetEvent)
        invokeCount += 1
        Console.WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.", 
                          DateTime.Now, invokeCount)
        If invokeCount = maxCount Then
            ' Reset the counter and signal the waiting thread.
            invokeCount = 0
            autoEvent.Set()
        End If
    End Sub
End Class
' 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.

설명

매개 변수로 callback 지정된 대리자는 경과 후 dueTime 한 번 호출되고, 그 후에는 시간 간격이 period 경과할 때마다 호출됩니다.

가 0 callback 인 경우 dueTime 는 즉시 호출됩니다. 가 이 Timeout.InfinitecallbackdueTime 가 호출되지 않고 타이머가 비활성화되지만 메서드를 호출 Change 하여 다시 사용하도록 설정할 수 있습니다.

클래스는 Timer Windows 7 및 Windows 8 시스템에서 callback 약 15밀리초인 시스템 클록과 동일한 해상도를 가지므로 이 시스템 클록의 해상도보다 작은 경우 period 대리자는 시스템 클록의 해상도로 정의된 간격으로 실행됩니다. 가 0이거나 Timeout.InfinitedueTime 이 아닌 callbackTimeout.Infinite경우 period 가 한 번 호출됩니다. 타이머의 주기적 동작은 사용하지 않도록 설정되지만 메서드를 사용하여 Change 다시 사용하도록 설정할 수 있습니다.

참고

사용되는 시스템 클록은 GetTickCount에서 사용하는 것과 동일한 클록으로, timeBeginPeriodtimeEndPeriod로 변경된 내용의 영향을 받지 않습니다.

callback 지정된 메서드는 스레드에서 ThreadPool 호출되므로 재진입해야 합니다. 타이머 간격이 메서드를 실행하는 데 필요한 시간보다 작거나 모든 스레드 풀 스레드가 사용 중이고 메서드가 여러 번 큐에 대기 중인 경우 두 스레드 풀 스레드에서 메서드를 동시에 실행할 수 있습니다.

추가 정보

적용 대상

Timer(TimerCallback, Object, Int64, Int64)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

부호 있는 64비트 정수로 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

public:
 Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, long dueTime, long period);
public Timer (System.Threading.TimerCallback callback, object? state, long dueTime, long period);
public Timer (System.Threading.TimerCallback callback, object state, long dueTime, long period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * int64 * int64 -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As Long, period As Long)

매개 변수

callback
TimerCallback

실행할 메서드를 나타내는 TimerCallback 대리자입니다.

state
Object

콜백 메서드에서 사용할 정보가 포함된 개체를 반환하거나 null을 반환합니다.

dueTime
Int64

callback이 호출되기 전에 지연할 시간(밀리초)입니다. 타이머가 시작되지 않도록 하려면 Infinite를 지정합니다. 타이머를 즉시 시작하려면 0을 지정합니다.

period
Int64

callback 호출 사이의 시간 간격(밀리초)입니다. 정기적으로 신호를 보내지 않도록 하려면 Infinite를 지정합니다.

예외

dueTime 또는 period 매개 변수가 음수이고 Infinite와 같지 않은 경우

dueTime 또는 period 매개 변수가 4294967294보다 큰 경우

설명

매개 변수로 callback 지정된 대리자는 경과 후 dueTime 한 번 호출되고, 그 후에는 시간 간격이 period 경과할 때마다 호출됩니다.

가 0 callback 인 경우 dueTime 는 즉시 호출됩니다. 가 이 Timeout.InfinitecallbackdueTime 가 호출되지 않고 타이머가 비활성화되지만 메서드를 호출 Change 하여 다시 사용하도록 설정할 수 있습니다.

클래스는 Timer Windows 7 및 Windows 8 시스템에서 callback 약 15밀리초인 시스템 클록과 동일한 해상도를 가지므로 이 시스템 클록의 해상도보다 작은 경우 period 대리자는 시스템 클록의 해상도로 정의된 간격으로 실행됩니다. 가 0이거나 Timeout.InfinitedueTime 이 아닌 callbackTimeout.Infinite경우 period 가 한 번 호출됩니다. 타이머의 주기적 동작은 사용하지 않도록 설정되지만 메서드를 사용하여 Change 다시 사용하도록 설정할 수 있습니다.

참고

사용되는 시스템 클록은 GetTickCount에서 사용하는 것과 동일한 클록으로, timeBeginPeriodtimeEndPeriod로 변경된 내용의 영향을 받지 않습니다.

callback 지정된 메서드는 스레드에서 ThreadPool 호출되므로 재진입해야 합니다. 타이머 간격이 메서드를 실행하는 데 필요한 시간보다 작거나 모든 스레드 풀 스레드가 사용 중이고 메서드가 여러 번 큐에 대기 중인 경우 두 스레드 풀 스레드에서 메서드를 동시에 실행할 수 있습니다.

추가 정보

적용 대상

Timer(TimerCallback, Object, TimeSpan, TimeSpan)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

TimeSpan 값을 사용하여 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

public:
 Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, TimeSpan dueTime, TimeSpan period);
public Timer (System.Threading.TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period);
public Timer (System.Threading.TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * TimeSpan * TimeSpan -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As TimeSpan, period As TimeSpan)

매개 변수

callback
TimerCallback

실행할 메서드를 나타내는 대리자입니다.

state
Object

콜백 메서드에서 사용할 정보가 포함된 개체를 반환하거나 null을 반환합니다.

dueTime
TimeSpan

callback이 호출되기 전에 지연할 시간입니다. 타이머가 시작되지 않도록 하려면 InfiniteTimeSpan를 지정합니다. 타이머를 즉시 시작하려면 Zero를 지정합니다.

period
TimeSpan

callback 호출 사이의 시간 간격입니다. 정기적으로 신호를 보내지 않도록 하려면 InfiniteTimeSpan를 지정합니다.

예외

또는 값의 밀리초가 음수이고 와 같지 Infinite않거나 Int32.MaxValue보다 큽니다.dueTimeperiod

callback 매개 변수가 null인 경우

예제

다음 코드 예제에서는 대리자를 TimerCallback 만들고 클래스의 Timer 새 instance 초기화하는 방법을 보여줍니다.

using namespace System;
using namespace System::Threading;
ref class StatusChecker
{
private:
   int invokeCount;
   int maxCount;

public:
   StatusChecker( int count )
      : invokeCount( 0 ), maxCount( count )
   {}


   // This method is called by the timer delegate.
   void CheckStatus( Object^ stateInfo )
   {
      AutoResetEvent^ autoEvent = dynamic_cast<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 main.
         invokeCount = 0;
         autoEvent->Set();
      }
   }

};

int main()
{
   AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false );
   StatusChecker^ statusChecker = gcnew StatusChecker( 10 );
   
   // Create the delegate that invokes methods for the timer.
   TimerCallback^ timerDelegate = gcnew TimerCallback( statusChecker, &StatusChecker::CheckStatus );
   TimeSpan delayTime = TimeSpan(0,0,1);
   TimeSpan intervalTime = TimeSpan(0,0,0,0,250);
   
   // Create a timer that signals the delegate to invoke CheckStatus 
   // after one second, and every 1/4 second thereafter.
   Console::WriteLine( "{0} Creating timer.\n", DateTime::Now.ToString(  "h:mm:ss.fff" ) );
   Timer^ stateTimer = gcnew Timer( timerDelegate,autoEvent,delayTime,intervalTime );
   
   // When autoEvent signals, change the period to every 1/2 second.
   autoEvent->WaitOne( 5000, false );
   stateTimer->Change( TimeSpan(0), intervalTime + intervalTime );
   Console::WriteLine( "\nChanging period.\n" );
   
   // When autoEvent signals the second time, dispose of the timer.
   autoEvent->WaitOne( 5000, false );
   stateTimer->~Timer();
   Console::WriteLine( "\nDestroying timer." );
}
using System;
using System.Threading;

class TimerExample
{
    static void Main()
    {
        AutoResetEvent autoEvent     = new AutoResetEvent(false);
        StatusChecker  statusChecker = new StatusChecker(10);

        // Create the delegate that invokes methods for the timer.
        TimerCallback timerDelegate = 
            new TimerCallback(statusChecker.CheckStatus);

        TimeSpan delayTime = new TimeSpan(0, 0, 1);
        TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 250);

        // Create a timer that signals the delegate to invoke 
        // CheckStatus after one second, and every 1/4 second 
        // thereafter.
        Console.WriteLine("{0} Creating timer.\n", 
            DateTime.Now.ToString("h:mm:ss.fff"));
        Timer stateTimer = new Timer(
            timerDelegate, autoEvent, delayTime, intervalTime);

        // When autoEvent signals, change the period to every 
        // 1/2 second.
        autoEvent.WaitOne(5000, false);
        stateTimer.Change(new TimeSpan(0), 
            intervalTime + intervalTime);
        Console.WriteLine("\nChanging period.\n");

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

class StatusChecker
{
    int invokeCount, 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 Main.
            invokeCount  = 0;
            autoEvent.Set();
        }
    }
}
Imports System.Threading

Public Class TimerExample

    <MTAThread> _
    Shared Sub Main()
    
        Dim autoEvent As New AutoResetEvent(False)
        Dim statusChecker As New StatusChecker(10)

        ' Create the delegate that invokes methods for the timer.
        Dim timerDelegate As TimerCallback = _
            AddressOf statusChecker.CheckStatus

        Dim delayTime As New TimeSpan(0, 0, 1)
        Dim intervalTime As New TimeSpan(0, 0, 0, 0, 250)

        ' Create a timer that signals the delegate to invoke 
        ' CheckStatus after one second, and every 1/4 second 
        ' thereafter.
        Console.WriteLine("{0} Creating timer." & vbCrLf, _
            DateTime.Now.ToString("h:mm:ss.fff"))
        Dim stateTimer As Timer = New Timer( _
            timerDelegate, autoEvent, delayTime, intervalTime)

        ' When autoEvent signals, change the period to every 
        ' 1/2 second.
        autoEvent.WaitOne(5000, False)
        stateTimer.Change( _
            new TimeSpan(0), intervalTime.Add(intervalTime))
        Console.WriteLine(vbCrLf & "Changing period." & vbCrLf)

        ' When autoEvent signals the second time, dispose of 
        ' the timer.
        autoEvent.WaitOne(5000, False)
        stateTimer.Dispose()
        Console.WriteLine(vbCrLf & "Destroying timer.")
    
    End Sub
End Class

Public Class StatusChecker

    Dim invokeCount, maxCount As Integer 

    Sub New(count As Integer)
        invokeCount  = 0
        maxCount = count
    End Sub

    ' This method is called by the timer delegate.
    Sub CheckStatus(stateInfo As Object)
        Dim autoEvent As AutoResetEvent = _
            DirectCast(stateInfo, AutoResetEvent)
        invokeCount += 1
        Console.WriteLine("{0} Checking status {1,2}.", _
            DateTime.Now.ToString("h:mm:ss.fff"), _
            invokeCount.ToString())

        If invokeCount = maxCount Then
        
            ' Reset the counter and signal to stop the timer.
            invokeCount  = 0
            autoEvent.Set()
        End If
    End Sub

End Class

설명

매개 변수로 callback 지정된 대리자는 경과 후 dueTime 한 번 호출되고, 그 후에는 시간 간격이 period 경과할 때마다 호출됩니다.

가 0 callback 인 경우 dueTime 는 즉시 호출됩니다. 가 음수인 경우 dueTime (-1) 밀리초가 callback 호출되지 않고 타이머가 사용하지 않도록 설정되지만 메서드를 호출 Change 하여 다시 사용하도록 설정할 수 있습니다.

클래스는 Timer Windows 7 및 Windows 8 시스템에서 callback 약 15밀리초인 시스템 클록과 동일한 해상도를 가지므로 이 시스템 클록의 해상도보다 작은 경우 period 대리자는 시스템 클록의 해상도로 정의된 간격으로 실행됩니다. 가 0 또는 음수(-1) 밀리초이고 dueTime 양수 callback 이면 period 한 번 호출됩니다. 타이머의 주기적 동작은 사용하지 않도록 설정되지만 메서드를 사용하여 Change 다시 사용하도록 설정할 수 있습니다.

참고

사용되는 시스템 클록은 GetTickCount에서 사용하는 것과 동일한 클록으로, timeBeginPeriodtimeEndPeriod로 변경된 내용의 영향을 받지 않습니다.

callback 지정된 메서드는 스레드에서 ThreadPool 호출되므로 재진입해야 합니다. 타이머 간격이 메서드를 실행하는 데 필요한 시간보다 작거나 모든 스레드 풀 스레드가 사용 중이고 메서드가 여러 번 큐에 대기 중인 경우 두 스레드 풀 스레드에서 메서드를 동시에 실행할 수 있습니다.

추가 정보

적용 대상

Timer(TimerCallback, Object, UInt32, UInt32)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

중요

이 API는 CLS 규격이 아닙니다.

부호 있는 32비트 정수로 시간 간격을 측정하여 Timer 클래스의 새 인스턴스를 초기화합니다.

public:
 Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, System::UInt32 dueTime, System::UInt32 period);
[System.CLSCompliant(false)]
public Timer (System.Threading.TimerCallback callback, object? state, uint dueTime, uint period);
[System.CLSCompliant(false)]
public Timer (System.Threading.TimerCallback callback, object state, uint dueTime, uint period);
[<System.CLSCompliant(false)>]
new System.Threading.Timer : System.Threading.TimerCallback * obj * uint32 * uint32 -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As UInteger, period As UInteger)

매개 변수

callback
TimerCallback

실행할 메서드를 나타내는 대리자입니다.

state
Object

콜백 메서드에서 사용할 정보가 포함된 개체를 반환하거나 null을 반환합니다.

dueTime
UInt32

callback이 호출되기 전에 지연할 시간(밀리초)입니다. 타이머가 시작되지 않도록 하려면 Infinite를 지정합니다. 타이머를 즉시 시작하려면 0을 지정합니다.

period
UInt32

callback 호출 사이의 시간 간격(밀리초)입니다. 정기적으로 신호를 보내지 않도록 하려면 Infinite를 지정합니다.

특성

예외

dueTime 또는 period 매개 변수가 음수이고 Infinite와 같지 않은 경우

callback 매개 변수가 null인 경우

설명

매개 변수로 callback 지정된 대리자는 경과 후 dueTime 한 번 호출되고, 그 후에는 시간 간격이 period 경과할 때마다 호출됩니다.

가 0 callback 인 경우 dueTime 는 즉시 호출됩니다. 가 이 Timeout.InfinitecallbackdueTime 가 호출되지 않고 타이머가 비활성화되지만 메서드를 호출 Change 하여 다시 사용하도록 설정할 수 있습니다.

클래스는 Timer Windows 7 및 Windows 8 시스템에서 callback 약 15밀리초인 시스템 클록과 동일한 해상도를 가지므로 이 시스템 클록의 해상도보다 작은 경우 period 대리자는 시스템 클록의 해상도로 정의된 간격으로 실행됩니다. 가 0이거나 Timeout.InfinitedueTime 아닌 callbackTimeout.Infinite경우 period 가 한 번 호출됩니다. 타이머의 주기적 동작은 사용하지 않도록 설정되지만 메서드를 사용하여 Change 다시 사용하도록 설정할 수 있습니다.

참고

사용되는 시스템 클록은 GetTickCount에서 사용하는 것과 동일한 클록으로, timeBeginPeriodtimeEndPeriod를 사용한 변경 내용의 영향을 받지 않습니다.

callback 지정된 메서드는 스레드에서 ThreadPool 호출되므로 재진입해야 합니다. 타이머 간격이 메서드를 실행하는 데 필요한 시간보다 작거나 모든 스레드 풀 스레드가 사용 중이고 메서드가 여러 번 큐에 대기되는 경우 메서드를 두 스레드 풀 스레드에서 동시에 실행할 수 있습니다.

추가 정보

적용 대상