Timer.Interval Timer.Interval Timer.Interval Timer.Interval Property

정의

Elapsed 이벤트를 발생시킬 간격(밀리초)을 가져오거나 설정합니다.Gets or sets the interval, expressed in milliseconds, at which to raise the Elapsed event.

public:
 property double Interval { double get(); void set(double value); };
[System.ComponentModel.SettingsBindable(true)]
[System.Timers.TimersDescription("The number of milliseconds between timer events.")]
[System.Timers.TimersDescription("TimerInterval")]
public double Interval { get; set; }
member this.Interval : double with get, set
Public Property Interval As Double

속성 값

Elapsed 이벤트 간의 시간(밀리초)입니다.The time, in milliseconds, between Elapsed events. 값은 0보다 크고 MaxValue보다 작거나 같아야 합니다.The value must be greater than zero, and less than or equal to MaxValue. 기본값은 100밀리초입니다.The default is 100 milliseconds.

예외

간격이 0 이하인 경우The interval is less than or equal to zero.

또는-or- 간격은 MaxValue 보다 크며, 타이머는 현재 사용 가능합니다.The interval is greater than MaxValue, and the timer is currently enabled. 타이머를 현재 사용할 수 없는 경우, 사용 가능하게 될 때까지 예외가 throw되지 않습니다.(If the timer is not currently enabled, no exception is thrown until it becomes enabled.)

예제

다음 예제에서는 Timer 발생 하는 개체는 Timer.Elapsed 2 초 (2000 밀리초) 마다 이벤트를 이벤트에 대 한 이벤트 처리기를 설정 하 고 타이머를 시작 합니다.The following example instantiates a Timer object that fires its Timer.Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. 값을 표시 하는 이벤트 처리기는 ElapsedEventArgs.SignalTime 될 때마다 발생 하는 속성입니다.The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised.

using namespace System;
using namespace System::Timers;

public ref class Example
{
private:
    static System::Timers::Timer^ aTimer;

public:
    static void Demo()
    {
        // Create a timer and set a two second interval.
        aTimer = gcnew System::Timers::Timer();
        aTimer->Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(Example::OnTimedEvent);

        // Have the timer fire repeated events (true is the default)
        aTimer->AutoReset = true;

        // Start the timer
        aTimer->Enabled = true;

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

private:
    static void OnTimedEvent(Object^ source, System::Timers::ElapsedEventArgs^ e)
    {
        Console::WriteLine("The Elapsed event was raised at {0}", e->SignalTime);
    }
};

int main()
{
    Example::Demo();
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM 

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

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

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM 
Imports System.Timers

Public Module Example
    Private aTimer As Timer

    Public Sub Main()
        ' Create a timer and set a two second interval.
        aTimer = New System.Timers.Timer()
        aTimer.Interval = 2000

        ' Hook up the Elapsed event for the timer.  
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = True

        ' Start the timer
        aTimer.Enabled = True

        Console.WriteLine("Press the Enter key to exit the program at any time... ")
        Console.ReadLine()
    End Sub

    Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
    End Sub
End Module
' The example displays output like the following: 
'       Press the Enter key to exit the program at any time... 
'       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:06 PM 

설명

사용할 합니다 Interval 는 빈도 결정 하는 속성을 Elapsed 이벤트가 발생 합니다.You use the Interval property to determine the frequency at which the Elapsed event is fired. 때문에 Timer 클래스 시스템 클록에 따라 달라 집니다, 시스템 클록으로 동일한 해상도 가진 것입니다.Because the Timer class depends on the system clock, it has the same resolution as the system clock. 즉 합니다 Elapsed 하는 경우 시스템 클록을 확인 하 여 정의 된 간격 마다이 이벤트가 발생 합니다는 Interval 속성을 사용 하면 시스템 클록의 해상도 보다 작습니다.This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock. 다음 예제에서는 Interval 속성을 5 개 시간 (밀리초)입니다.The following example sets the Interval property to 5 milliseconds. 실행할 때를 Windows 7Windows 7 시스템 클럭의 5 밀리초 마다 보다는 약 15 밀리초 마다 약 15 밀리초, 이벤트가 발생 해상도 포함 하는 시스템입니다.When run on a Windows 7Windows 7 system whose system clock has a resolution of approximately 15 milliseconds, the event fires approximately every 15 milliseconds rather than every 5 milliseconds.

using System;
using System.IO;
using System.Collections.Generic;
using System.Timers;

public class Example
{
   private static Timer aTimer;
   private static List<String> eventlog;
   private static int nEventsFired = 0;
   private static DateTime previousTime;
       
   public static void Main()
   {
        eventlog = new List<String>();
        
        StreamWriter sr = new StreamWriter(@".\Interval.txt");
        // Create a timer with a five millisecond interval.
        aTimer = new Timer(5);
        aTimer.Elapsed += OnTimedEvent;
        // Hook up the Elapsed event for the timer. 
        aTimer.AutoReset = true;
        sr.WriteLine("The timer should fire every {0} milliseconds.", 
                     aTimer.Interval);
        aTimer.Enabled = true;

        
        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        foreach (var item in eventlog)
           sr.WriteLine(item);
        sr.Close();
        Console.WriteLine("Terminating the application...");
   }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})", 
                                   e.SignalTime, 
                                   nEventsFired++ == 0 ? 
                                      0.0 : (e.SignalTime - previousTime).TotalMilliseconds));
        previousTime = e.SignalTime;
        if (nEventsFired == 20) {
           Console.WriteLine("No more events will fire...");
           aTimer.Enabled = false;
        }
    }
}
// The example writes output like the following to a file:
//       The timer should fire every 5 milliseconds.
//       Elapsed event at 08:42:49.370344 (0)
//       Elapsed event at 08:42:49.385345 (15.0015)
//       Elapsed event at 08:42:49.400347 (15.0015)
//       Elapsed event at 08:42:49.415348 (15.0015)
//       Elapsed event at 08:42:49.430350 (15.0015)
//       Elapsed event at 08:42:49.445351 (15.0015)
//       Elapsed event at 08:42:49.465353 (20.002)
//       Elapsed event at 08:42:49.480355 (15.0015)
//       Elapsed event at 08:42:49.495356 (15.0015)
//       Elapsed event at 08:42:49.510358 (15.0015)
//       Elapsed event at 08:42:49.525359 (15.0015)
//       Elapsed event at 08:42:49.540361 (15.0015)
//       Elapsed event at 08:42:49.555362 (15.0015)
//       Elapsed event at 08:42:49.570364 (15.0015)
//       Elapsed event at 08:42:49.585365 (15.0015)
//       Elapsed event at 08:42:49.605367 (20.002)
//       Elapsed event at 08:42:49.620369 (15.0015)
//       Elapsed event at 08:42:49.635370 (15.0015)
//       Elapsed event at 08:42:49.650372 (15.0015)
//       Elapsed event at 08:42:49.665373 (15.0015)
Imports System.Collections.Generic
Imports System.IO
Imports System.Timers

Module Example
   Private WithEvents aTimer As Timer
   Private eventlog As List(Of String)
   Private nEventsFired As Integer = 0
   Private previousTime As Date

   Public Sub Main()
        eventlog = New List(Of String)()
        
        Dim sr As New StreamWriter(".\Interval.txt")
        ' Create a timer with a five millisecond interval.
        aTimer = New Timer(5)
        aTimer.AutoReset = True
        sr.WriteLine("The timer should fire every {0} milliseconds.", 
                     aTimer.Interval)
        aTimer.Enabled = True

        
        Console.WriteLine("Press the Enter key to exit the program... ")
        Console.ReadLine()
        For Each item In eventlog
           sr.WriteLine(item)
        Next
        sr.Close()
        Console.WriteLine("Terminating the application...")
   End Sub

    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) _
                             Handles aTimer.Elapsed
        eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})", 
                                   e.SignalTime, 
                                   If(nEventsFired = 0, 
                                      0.0, (e.SignalTime - previousTime).TotalMilliseconds)))
        nEventsFired += 1
        previousTime = e.SignalTime
        if nEventsFired = 20 Then
           Console.WriteLine("No more events will fire...")
           aTimer.Enabled = False
        End If
   End Sub
End Module
' The example displays the following output:
'       The timer should fire every 5 milliseconds.
'       Elapsed event at 08:42:49.370344 (0)
'       Elapsed event at 08:42:49.385345 (15.0015)
'       Elapsed event at 08:42:49.400347 (15.0015)
'       Elapsed event at 08:42:49.415348 (15.0015)
'       Elapsed event at 08:42:49.430350 (15.0015)
'       Elapsed event at 08:42:49.445351 (15.0015)
'       Elapsed event at 08:42:49.465353 (20.002)
'       Elapsed event at 08:42:49.480355 (15.0015)
'       Elapsed event at 08:42:49.495356 (15.0015)
'       Elapsed event at 08:42:49.510358 (15.0015)
'       Elapsed event at 08:42:49.525359 (15.0015)
'       Elapsed event at 08:42:49.540361 (15.0015)
'       Elapsed event at 08:42:49.555362 (15.0015)
'       Elapsed event at 08:42:49.570364 (15.0015)
'       Elapsed event at 08:42:49.585365 (15.0015)
'       Elapsed event at 08:42:49.605367 (20.002)
'       Elapsed event at 08:42:49.620369 (15.0015)
'       Elapsed event at 08:42:49.635370 (15.0015)
'       Elapsed event at 08:42:49.650372 (15.0015)
'       Elapsed event at 08:42:49.665373 (15.0015)

현재 시스템에서 시스템 클록의 해상도 결정 하려면 다음 코드를 사용할 수 있습니다.You can use the following code to determine the resolution of the system clock on the current system:

using System;
using System.Runtime.InteropServices;

public class Example
{
   [DllImport("kernel32.dll", SetLastError=true)]
   static extern bool GetSystemTimeAdjustment(out long lpTimeAdjustment,
                                              out long lpTimeIncrement,
                                              out bool lpTimeAdjustmentDisabled);
   
   public static void Main()
   {
      long timeAdjustment, timeIncrement = 0;
      bool timeAdjustmentDisabled;
      
      if (GetSystemTimeAdjustment(out timeAdjustment, out timeIncrement, 
                                  out timeAdjustmentDisabled)) {
         if (! timeAdjustmentDisabled)
            Console.WriteLine("System clock resolution: {0:N3} milliseconds", 
                              timeIncrement/10000.0);
         else
            Console.WriteLine("Unable to determine system clock resolution.");                     
      }
   }      
}
// The example displays output like the following:
//        System clock resolution: 15.600 milliseconds
Module Example
   Private Declare Function GetSystemTimeAdjustment Lib "Kernel32" (
                   ByRef lpTimeAdjustment As Long, ByRef lpTimeIncrement As Long,
                   ByRef lpTimeAdjustmentDisabled As Boolean) As Boolean

   Public Sub Main()
      Dim timeAdjustment, timeIncrement As Long
      Dim timeAdjustmentDisabled As Boolean
      
      If GetSystemTimeAdjustment(timeAdjustment, timeIncrement, 
                                  timeAdjustmentDisabled) Then
         If Not timeAdjustmentDisabled Then
            Console.WriteLine("System clock resolution: {0:N3} milliseconds", 
                              timeIncrement/10000.0)
         Else
            Console.WriteLine("Unable to determine system clock resolution.")                     
         End If
      End If
   End Sub
End Module
' The example displays output similar to the following:
'       System clock resolution: 15.625 milliseconds

앱이 Timer 클래스 또는 시스템 클록에서 제공 하는 것 보다 더 높은 해상도를 필요로 하는 경우 고해상도 멀티미디어 타이머를 사용 합니다 . 자세한 내용은 다음을 참조 하세요. 고해상도 타이머를 사용 합니다.If your app requires greater resolution than that offered by the Timer class or the system clock, use the high-resolution multimedia timers; see How to: Use the High-Resolution Timer.

간격 후 설정 된 경우는 Timer 에 시작 수 다시 설정 됩니다.If the interval is set after the Timer has started, the count is reset. 예를 들어 간격을 5 초로 설정 하 고 다음을 설정 합니다 Enabled 속성을 true, 수 시간에 시작 Enabled 설정 됩니다.For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. 때 3 초, 10 초 간격을 다시 설정 합니다 Elapsed 13 초 후 처음으로 이벤트가 발생 Enabled 로 설정 된 true합니다.If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

경우 Enabled 로 설정 된 trueAutoReset 로 설정 되어 falseTimer 발생 시킵니다는 Elapsed 첫 번째 시간 간격을 한 번만 이벤트 경과 합니다.If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses. Enabled 그런 다음로 false합니다.Enabled is then set to false.

참고

경우 EnabledAutoReset 로 설정 됩니다 false, 및 타이머 이전에 활성화 되어 있는 설정 합니다 Interval 속성 원인 합니다 Elapsed 이벤트를 한 번 발생 처럼는 Enabled 로설정된속성true.If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. 간격 이벤트를 발생 하지 않고을 설정 하려면 설정할 수 있습니다 일시적으로 Enabled 속성을 true설정 합니다 Interval 속성을 원하는 시간 간격을 즉시 설정한 후를 Enabled 속성을 다시 false.To set the interval without raising the event, you can temporarily set the Enabled property to true, set the Interval property to the desired time interval, and then immediately set the Enabled property back to false.

적용 대상

추가 정보