次の方法で共有


Timer.Interval プロパティ

Elapsed イベントの発生間隔を取得または設定します。

Public Property Interval As Double
[C#]
public double Interval {get; set;}
[C++]
public: __property double get_Interval();public: __property void set_Interval(double);
[JScript]
public function get Interval() : double;public function set Interval(double);

プロパティ値

ミリ秒単位での Elapsed イベントの発生間隔。既定値は 100 ミリ秒です。

例外

例外の種類 条件
ArgumentException 間隔が 0 未満です。

解説

Timer が開始した後に間隔を設定すると、カウントがリセットされます。たとえば、間隔を 5 秒に設定し、 Enabledtrue に設定した場合は、 Enabled を設定した時刻からカウントが開始します。カウントが 3 秒のときに間隔を 10 秒にリセットした場合、最初の Elapsed イベントは Enabledtrue に設定してから 13 秒後に発生します。

Enabledtrue に設定し、 AutoResetfalse に設定した場合、 Timer は最初に間隔が経過したときに、1 回だけ Elapsed イベントを発生させます。

使用例

[Visual Basic, C#, C++] 5 秒ごとに、コンソールに "Hello World!" と表示する Timer を作成する例を次に示します。

[Visual Basic, C#, C++] この例では、 System.Timers 名前空間を使用します。

 
Public Class Timer1
    
    Public Shared Sub Main()
        Dim aTimer As New System.Timers.Timer()
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Set the Interval to 5 seconds.
        aTimer.Interval = 5000
        aTimer.Enabled = True
        
        Console.WriteLine("Press 'q' to quit the sample.")
        While Console.Read() <> CInt("q")
        End While
    End Sub
        
    ' Specify what you want to happen when the Elapsed event is raised.
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class


[C#] 
public class Timer1
 {
 
     public static void Main()
     {
         System.Timers.Timer aTimer = new System.Timers.Timer();
         aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
         // Set the Interval to 5 seconds.
         aTimer.Interval=5000;
         aTimer.Enabled=true;
 
         Console.WriteLine("Press \'q\' to quit the sample.");
         while(Console.Read()!='q');
     }
 
     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         Console.WriteLine("Hello World!");
     }
 }
 

[C++] 
public __gc class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Timer クラス | Timer メンバ | System.Timers 名前空間 | AutoReset | Enabled | Elapsed