英語で読む

次の方法で共有


Timer コンストラクター

定義

重要

一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。

Timer クラスの新しいインスタンスを初期化します。

オーバーロード

Timer()

Timer クラスの新しいインスタンスを初期化し、すべてのプロパティを初期値に設定します。

Timer(Double)

Timer クラスの新しいインスタンスを初期化し、Interval プロパティを指定したミリ秒数に設定します。

Timer(TimeSpan)

プロパティを指定した期間に設定して Timer 、 クラスの Interval 新しいインスタンスを初期化します。

Timer()

ソース:
Timer.cs
ソース:
Timer.cs
ソース:
Timer.cs

Timer クラスの新しいインスタンスを初期化し、すべてのプロパティを初期値に設定します。

public Timer ();

次の例では、 Timer 2 秒 (2000 ミリ秒) ごとにイベントを Timer.Elapsed 発生させ、イベントのイベント ハンドラーを設定し、タイマーを開始する オブジェクトをインスタンス化します。 イベント ハンドラーは、プロパティが発生するたびにプロパティの値を ElapsedEventArgs.SignalTime 表示します。

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

注釈

のインスタンスの初期プロパティ値を次の Timer表に示します。

プロパティ 初期値
AutoReset true
Enabled false
Interval 100 ミリ秒
SynchronizingObject null 参照 (Visual Basic の場合は Nothing)。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Timer(Double)

ソース:
Timer.cs
ソース:
Timer.cs
ソース:
Timer.cs

Timer クラスの新しいインスタンスを初期化し、Interval プロパティを指定したミリ秒数に設定します。

public Timer (double interval);

パラメーター

interval
Double

ミリ秒単位でのイベントの発生間隔。 値は 0 より大きく、 Int32.MaxValue 以下である必要があります。

例外

パラメーターの interval 値が 0 以下か、 Int32.MaxValue より大きい値です。

次の例では、 Timer 2 秒 (2000 ミリ秒) ごとにイベントを Timer.Elapsed 発生させ、イベントのイベント ハンドラーを設定し、タイマーを開始する オブジェクトをインスタンス化します。 イベント ハンドラーは、プロパティが発生するたびにプロパティの値を ElapsedEventArgs.SignalTime 表示します。

using System;
using System.Threading.Tasks;
using System.Timers;

class Example
{
   static void Main()
   {
      Timer timer = new Timer(1000);
      timer.Elapsed += async ( sender, e ) => await HandleTimer();
      timer.Start();
      Console.Write("Press any key to exit... ");
      Console.ReadKey();
   }

   private static Task HandleTimer()
   {
     Console.WriteLine("\nHandler not implemented..." );
     throw new NotImplementedException();
   }
}
// The example displays output like the following:
//   Press any key to exit...
//   Handler not implemented...
//   
//   Unhandled Exception: System.NotImplementedException: The method or operation is not implemented.
//      at Example.HandleTimer()
//      at Example.<<Main>b__0>d__2.MoveNext()
//   --- End of stack trace from previous location where exception was thrown ---
//      at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass2.<ThrowAsync>b__5(Object state)
//      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
//      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
//      at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
//      at System.Threading.ThreadPoolWorkQueue.Dispatch()

注釈

このコンストラクターは、新しいタイマー インスタンスの プロパティを設定 Interval しますが、タイマーは有効になりません。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Timer(TimeSpan)

ソース:
Timer.cs
ソース:
Timer.cs
ソース:
Timer.cs

プロパティを指定した期間に設定して Timer 、 クラスの Interval 新しいインスタンスを初期化します。

public Timer (TimeSpan interval);

パラメーター

interval
TimeSpan

イベント間の時間。 ミリ秒単位の値は、0 より大きく、 以下 MaxValueである必要があります。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9