Timer で Elapsed イベントを発生させる必要があるかどうかを示す値を取得または設定します。
Public Property Enabled As Boolean
[C#]
public bool Enabled {get; set;}
[C++]
public: __property bool get_Enabled();public: __property void set_Enabled(bool);
[JScript]
public function get Enabled() : Boolean;public function set Enabled(Boolean);
プロパティ値
Timer で Elapsed イベントを発生させる場合は true 。それ以外の場合は false 。既定値は true です。
解説
Enabled を true に設定することは、 Start を呼び出すことと同じであり、 Enabled を false に設定することは、 Stop を呼び出すことと同じです。
Enabled を true に設定し、 AutoReset を false に設定した場合、 Timer は最初に間隔が経過したときに、1 回だけ Elapsed イベントを発生させます。
Timer が開始した後に間隔を設定すると、カウントがリセットされます。たとえば、間隔を 5 秒に設定し、 Enabled を true に設定した場合は、 Enabled を設定した時刻からカウントが開始します。カウントが 3 秒のときに間隔を 10 秒にリセットした場合、最初の Elapsed イベントは Enabled を true に設定してから 13 秒後に発生します。
使用例
[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 名前空間 | Interval | AutoReset | Start | Stop | Elapsed