다음을 통해 공유


Timer.Interval 속성

정의

Elapsed 이벤트를 발생시킬 간격(밀리초)을 가져오거나 설정합니다.

public:
 property double Interval { double get(); void set(double value); };
public double Interval { get; set; }
[System.Timers.TimersDescription("TimerInterval")]
public double Interval { get; set; }
[System.Timers.TimersDescription("TimerInterval")]
[System.ComponentModel.SettingsBindable(true)]
public double Interval { get; set; }
member this.Interval : double with get, set
[<System.Timers.TimersDescription("TimerInterval")>]
member this.Interval : double with get, set
[<System.Timers.TimersDescription("TimerInterval")>]
[<System.ComponentModel.SettingsBindable(true)>]
member this.Interval : double with get, set
Public Property Interval As Double

속성 값

Elapsed 이벤트 간의 시간(밀리초)입니다. 값은 0보다 크고 Int32.MaxValue보다 작거나 같아야 합니다. 기본값은 100밀리초입니다.

특성

예외

간격이 0 이하인 경우

또는

간격이 Int32.MaxValue보다 크며 타이머가 현재 활성화되어 있습니다. 타이머를 현재 사용할 수 없는 경우, 사용 가능하게 될 때까지 예외가 throw되지 않습니다.

예제

다음 예제에서는 2초마다 이벤트를 발생 Timer.Elapsed 시키는 개체를 인스턴스화 Timer 하고(2000밀리초) 이벤트에 대한 이벤트 처리기를 설정하고 타이머를 시작합니다. 이벤트 처리기는 발생할 때마다 속성의 ElapsedEventArgs.SignalTime 값을 표시합니다.

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
open System.Timers

let onTimedEvent source (e: ElapsedEventArgs) =
    printfn $"The Elapsed event was raised at {e.SignalTime}"

// Create a timer and set a two second interval.
let aTimer = new Timer()
aTimer.Interval <- 2000

// Hook up the Elapsed event for the timer. 
aTimer.Elapsed.AddHandler onTimedEvent

// Have the timer fire repeated events (true is the default)
aTimer.AutoReset <- true

// Start the timer
aTimer.Enabled <- true

printfn "Press the Enter key to exit the program at any time... "
stdin.ReadLine() |> ignore

// 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 빈도 결정 합니다. 클래스는 Timer 시스템 클록에 따라 달라지므로 시스템 클록과 해상도가 동일합니다. 즉, 속성이 Elapsed 시스템 클록의 해상도보다 작으면 Interval 시스템 클록의 해상도로 정의된 간격으로 이벤트가 발생합니다. 다음 예제에서는 Interval 속성을 5밀리초로 설정합니다. 시스템 클록의 해상도가 약 15밀리초인 Windows 시스템에서 실행되는 경우 이벤트는 5밀리초가 아닌 약 15밀리초마다 발생합니다.

참고

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

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)
open System
open System.IO
open System.Timers

let aTimer = new Timer 5
let eventlog = ResizeArray()
let mutable nEventsFired = 0
let mutable previousTime = DateTime()
       
let onTimedEvent source (e: ElapsedEventArgs) =
    String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})", e.SignalTime, if nEventsFired = 0 then 0. else (e.SignalTime - previousTime).TotalMilliseconds)
    |> eventlog.Add
    nEventsFired <- nEventsFired + 1

    previousTime <- e.SignalTime

    if nEventsFired = 20 then
        printfn "No more events will fire..."
        aTimer.Enabled <- false

[<EntryPoint>]
let main _ =
    use sr = new StreamWriter(@".\Interval.txt")
    // Create a timer with a five millisecond interval.
    aTimer.Elapsed.AddHandler onTimedEvent
    // Hook up the Elapsed event for the timer. 
    aTimer.AutoReset <- true
    sr.WriteLine $"The timer should fire every {aTimer.Interval} milliseconds."
    aTimer.Enabled <- true

    printfn "Press the Enter key to exit the program... "
    stdin.ReadLine() |> ignore
    for item in eventlog do
        sr.WriteLine item
    printfn "Terminating the application..."
    0

// 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)

앱에 클래스 또는 시스템 클록에서 제공하는 Timer 것보다 더 큰 해상도가 필요한 경우 고해상도 멀티미디어 타이머를 사용합니다. 방법: High-Resolution 타이머 사용을 참조하세요.

가 시작된 후 간격이 Timer 설정되면 개수가 다시 설정됩니다. 예를 들어 간격을 5초로 설정한 다음 속성을 true로 설정 Enabled 하면 횟수가 설정된 시간에 Enabled 시작됩니다. count가 3초일 때 간격을 10초 Elapsed 로 다시 설정하면 가 로 설정된 후 처음으로 13초 동안 Enabled 이벤트가 발생합니다 true.

가 로 true 설정되고 AutoReset 가 로 설정된 false경우 EnabledTimer 간격이 Elapsed 처음 경과할 때 이벤트를 한 번만 발생합니다. Enabled 가 로 설정됩니다 false.

참고

AutoReset 가 모두 로 설정되어 false있고 타이머가 이전에 사용하도록 설정된 경우 속성을 설정 Interval 하면 EnabledElapsed 속성이 로 설정된 것처럼 Enabled 이벤트가 한 번 발생합니다true. 이벤트를 발생 하지 않고 간격을 설정 하려면 일시적으로 설정 Enabled 된 속성을 true, 원하는 시간 간격으로 속성을 설정 Interval 하 고 즉시 다시 속성을 설정할 Enabledfalse있습니다.

적용 대상

추가 정보