Share via


方法 : High-Resolution、タイマーを使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

一部のデバイスを高分解能タイマーをサポートします。 このタイマーと存在、入手するには 1 ミリ秒解像度を持つ、TickCount プロパティを使用してより正確な値に提供されます。 正確な時間の単位が重要なアプリケーションで、高分解能タイマー最適な結果を提供します。 たとえば、一部の Direct3D アプリケーション表示よりスムーズにときに、アニメーションは、高分解能タイマーに基づいています。 メソッドまたはコードのセクションの実行に必要な時間を決定するのにアプリケーションでこのタイマーを使用することもできます。

使用例

このコード例は簡単に Windows CE でマネージ コードで高解像度のタイマーを使用するクラスを示します。 この例は、次のような機能。

  • プラットフォーム呼び出しを Windows CE のネイティブ メソッドの宣言。

  • 高解像度、カウンターの頻度の取得に使用されるプロパティ。

  • 高解像度、カウンターの値の取得に使用されるプロパティ。

  • TickCount 関数はサポートされていませんかは、エミュレートされたフォールバックとして、 QueryPerformanceCounter プロパティをサポートする実装します。

  • 高解像度のカウンターを使用して時間を操作する方法の例です。

                        Public
                        Class HiResTimer
    Private isPerfCounterSupported AsBoolean = FalsePrivate timerFrequency As Int64 = 0

    ' Windows CE native library with QueryPerformanceCounter().PrivateConst [lib] AsString = "coredll.dll"PublicDeclareFunction QueryPerformanceCounter Lib"Coredll.dll" _
    (ByRef count As Int64) AsIntegerPublicDeclareFunction QueryPerformanceFrequency Lib"Coredll.dll" _
    (ByRef timerFrequency As Int64) AsIntegerPublicSubNew() 
        ' Query the high-resolution timer only if it is supported.        ' A returned frequency of 1000 typically indicates that it is not        ' supported and is emulated by the OS using the same value that is        ' returned by Environment.TickCount.        ' A return value of 0 indicates that the performance counter is        ' not supported.Dim returnVal AsInteger = QueryPerformanceFrequency(timerFrequency)

        If returnVal <> 0 AndAlso timerFrequency <> 1000 Then            ' The performance counter is supported.
            isPerfCounterSupported = TrueElse            ' The performance counter is not supported. Use            ' Environment.TickCount instead.
            timerFrequency = 1000
        EndIfEndSubPublicReadOnlyProperty Frequency() As Int64 
        GetReturn timerFrequency
        EndGetEndPropertyPublicReadOnlyProperty Value() As Int64 
        GetDim tickCount As Int64 = 0

            If isPerfCounterSupported Then                ' Get the value here if the counter is supported.
                QueryPerformanceCounter(tickCount)
                Return tickCount
            Else                ' Otherwise, use Environment.TickCountReturnCType(Environment.TickCount, Int64)
            EndIfEndGetEndPropertySharedSub Main() 
        Dim timer AsNew HiResTimer()

        ' This example shows how to use the high-resolution counter to         ' time an operation.         ' Get counter value before the operation starts.Dim counterAtStart As Int64 = timer.Value

        ' Perform an operation that takes a measureable amount of time.Dim count AsIntegerFor count = 0 To 9999
            count += 1
            count -= 1
        Next count

        ' Get counter value after the operation ends.Dim counterAtEnd As Int64 = timer.Value

        ' Get time elapsed in tenths of millisecondsDim timeElapsedInTicks As Int64 = counterAtEnd - counterAtStart
        Dim timeElapseInTenthsOfMilliseconds As Int64 = timeElapsedInTicks * 10000 / timer.Frequency


        MessageBox.Show("Time Spent in operation (tenths of ms) " + timeElapseInTenthsOfMilliseconds.ToString + vbLf + "Counter Value At Start: " + counterAtStart.ToString + vbLf + "Counter Value At End : " + counterAtEnd.ToString + vbLf + "Counter Frequency : " + timer.Frequency.ToString)

    EndSubEndClass
                        public
                        class HiResTimer
{
    privatebool isPerfCounterSupported = false;
    private Int64 frequency = 0;

    // Windows CE native library with QueryPerformanceCounter().privateconststring lib = "coredll.dll";
    [DllImport(lib)]
    privatestaticexternint QueryPerformanceCounter(ref Int64 count);
    [DllImport(lib)]
    privatestaticexternint QueryPerformanceFrequency(ref Int64 frequency);

    public HiResTimer()
    {
        // Query the high-resolution timer only if it is supported.// A returned frequency of 1000 typically indicates that it is not// supported and is emulated by the OS using the same value that is// returned by Environment.TickCount.// A return value of 0 indicates that the performance counter is// not supported.int returnVal = QueryPerformanceFrequency(ref frequency);

        if (returnVal != 0 && frequency != 1000)
        {
            // The performance counter is supported.
            isPerfCounterSupported = true;
        }
        else
        {
            // The performance counter is not supported. Use// Environment.TickCount instead.
            frequency = 1000;
        }
    }

    public Int64 Frequency
    {
        get
        {
            return frequency;
        }
    }

    public Int64 Value
    {
        get
        {
            Int64 tickCount = 0;

            if (isPerfCounterSupported)
            {
                // Get the value here if the counter is supported.
                QueryPerformanceCounter(ref tickCount);
                return tickCount;
            }
            else
            {
                // Otherwise, use Environment.TickCount.return (Int64)Environment.TickCount;
            }
        }
    }

    staticvoid Main()
    {
        HiResTimer timer = new HiResTimer();

        // This example shows how to use the high-resolution counter to // time an operation. // Get counter value before the operation starts.
        Int64 counterAtStart = timer.Value;

        // Perform an operation that takes a measureable amount of time.for (int count = 0; count < 10000; count++)
        {
            count++;
            count--;
        }

        // Get counter value when the operation ends.
        Int64 counterAtEnd = timer.Value;

        // Get time elapsed in tenths of a millisecond.
        Int64 timeElapsedInTicks = counterAtEnd - counterAtStart;
        Int64 timeElapseInTenthsOfMilliseconds =
            (timeElapsedInTicks * 10000) / timer.Frequency;

        MessageBox.Show("Time Spent in operation (tenths of ms) "
                       + timeElapseInTenthsOfMilliseconds +
                       "\nCounter Value At Start: " + counterAtStart +
                       "\nCounter Value At End : " + counterAtEnd +
                       "\nCounter Frequency : " + timer.Frequency);
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

その他の技術情報

アプリケーションおよび .NET Compact Framework のコア タスクの構築

.NET Compact Framework の相互運用性