次の方法で共有


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 より大きく、タイマーは現在有効になっています。 (タイマーが現在有効になっていない場合は、有効になるまで例外はスローされません。)

次の例では、 Timer 2 秒 (2000 ミリ秒) ごとにイベントを Timer.Elapsed 発生させ、イベントのイベント ハンドラーを設定し、タイマーを開始する オブジェクトをインスタンス化します。 イベント ハンドラーは、プロパティが発生するたびにプロパティの値を 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クラスまたはシステム クロック、高解像度のマルチ メディア タイマーを使用して、参照してください。メソッド: 高分解能タイマを使用するします。

が開始された後に間隔が Timer 設定されている場合、カウントはリセットされます。 たとえば、間隔を 5 秒に設定し、 プロパティを Enabledtrue設定すると、カウントは設定時 Enabled に開始されます。 count が 3 秒のときに間隔を 10 秒にリセットすると、 Elapsed が にtrue設定された後、13 秒後Enabledにイベントが初めて発生します。

が にtrue設定され、 AutoReset が にfalse設定されている場合EnabledTimer は、最初に間隔がElapsed経過した時点でイベントを 1 回だけ発生させます。 Enabled は に設定されます false

注意

AutoReset の両方が にfalse設定されていて、タイマーが以前に有効になっている場合Enabled、 プロパティをInterval設定するとElapsed、 プロパティが に設定されているかのようにEnabled、イベントが 1 回発生しますtrue。 イベントを発生させずに間隔を設定するには、 プロパティを Enabled に一時的に設定し、プロパティ trueInterval 目的の時間間隔に設定してから、すぐに プロパティを Enabled に設定し false直します。

適用対象

こちらもご覧ください