PerformanceCounter.NextValue 方法

获取计数器样本并为其返回计算所得值。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public Function NextValue As Single
用法
Dim instance As PerformanceCounter
Dim returnValue As Single

returnValue = instance.NextValue
public float NextValue ()
public:
float NextValue ()
public float NextValue ()
public function NextValue () : float

返回值

系统为此计数器获取的下一计算所得值。

异常

异常类型 条件

InvalidOperationException

此实例未与性能计数器正确关联。

Win32Exception

访问系统 API 时出错。

PlatformNotSupportedException

平台为 Windows 98 或 Windows Millennium Edition (Me),这些平台不支持性能计数器。

备注

提示

如果计数器的计算所得值取决于两个计数器读数,则第一个读数返回 0.0。

示例

Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Public Class App
   
   Public Shared Sub Main()
      CollectSamples()
   End Sub
   
   Private Shared Sub CollectSamples()

      Dim categoryName as String = "ElapsedTimeSampleCategory"
      Dim counterName as String = "ElapsedTimeSample"

      If Not PerformanceCounterCategory.Exists(categoryName) Then
         
         Dim CCDC As New CounterCreationDataCollection()
         
         ' Add the counter.
         Dim ETimeData As New CounterCreationData()
         ETimeData.CounterType = PerformanceCounterType.ElapsedTime
         ETimeData.CounterName = counterName
         CCDC.Add(ETimeData)
         
         ' Create the category.
         PerformanceCounterCategory.Create(categoryName, _
            "Demonstrates ElapsedTime performance counter usage.", CCDC)
       
      Else
         Console.WriteLine("Category exists - {0}", categoryName)
      End If

      ' Create the counter.
      Dim PC As PerformanceCounter
      PC = New PerformanceCounter(categoryName, counterName, False)
     
      ' Initialize the counter.
      PC.RawValue = Stopwatch.GetTimestamp()

      Dim Start As DateTime = DateTime.Now
      
      ' Loop for the samples.
      Dim j As Integer
      For j = 0 To 99
         ' Output the values.
         If j Mod 10 = 9 Then
            Console.WriteLine(("NextValue() = " _
                + PC.NextValue().ToString()))
            Console.WriteLine(("Actual elapsed time = " _
                + DateTime.Now.Subtract(Start).ToString()))
            OutputSample(PC.NextSample())
         End If
         
         ' Reset the counter every 20th iteration.
         If j Mod 20 = 0 Then
            PC.RawValue = Stopwatch.GetTimestamp()
            Start = DateTime.Now
         End If
         System.Threading.Thread.Sleep(50)
      Next j
      
      Console.WriteLine(("Elapsed time = " + _
            DateTime.Now.Subtract(Start).ToString()))
   End Sub 
   
   
   Private Shared Sub OutputSample(s As CounterSample)
      Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "+++++++")

      Console.WriteLine("Sample values - " + ControlChars.Cr _
            + ControlChars.Lf)
      Console.WriteLine(("   BaseValue        = " _
            + s.BaseValue.ToString()))
      Console.WriteLine(("   CounterFrequency = " + _
            s.CounterFrequency.ToString()))
      Console.WriteLine(("   CounterTimeStamp = " + _
            s.CounterTimeStamp.ToString()))
      Console.WriteLine(("   CounterType      = " + _
            s.CounterType.ToString()))
      Console.WriteLine(("   RawValue         = " + _
            s.RawValue.ToString()))
      Console.WriteLine(("   SystemFrequency  = " + _
            s.SystemFrequency.ToString()))
      Console.WriteLine(("   TimeStamp        = " + _
            s.TimeStamp.ToString()))
      Console.WriteLine(("   TimeStamp100nSec = " + _
            s.TimeStamp100nSec.ToString()))
      
      Console.WriteLine("+++++++")
   End Sub
End Class
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class App 
{

    public static void Main()
    {   
        CollectSamples();
    }

   
    public static void CollectSamples()
    {
        const String categoryName = "ElapsedTimeSampleCategory";
        const String counterName = "ElapsedTimeSample";

        if ( !PerformanceCounterCategory.Exists(categoryName) ) 
        {

            CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData ETimeData = new CounterCreationData();
            ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
            ETimeData.CounterName = counterName;
            CCDC.Add(ETimeData);       
        
            // Create the category.
            PerformanceCounterCategory.Create(categoryName, 
                    "Demonstrates ElapsedTime performance counter usage.",
                    CCDC);

        }
        else
        {
            Console.WriteLine("Category exists - {0}", categoryName);
        }        

        // Create the performance counter.
        PerformanceCounter PC = new PerformanceCounter(categoryName, 
                                                       counterName, 
                                                       false);
        // Initialize the counter.
        PC.RawValue = Stopwatch.GetTimestamp();

        DateTime Start = DateTime.Now;

        // Loop for the samples.
        for (int j = 0; j < 100; j++) 
        {
            // Output the values.
            if ((j % 10) == 9) 
            {
                Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
                Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());
                OutputSample(PC.NextSample());
            }

            // Reset the counter on every 20th iteration.
            if (j % 20 == 0)
            {
                PC.RawValue = Stopwatch.GetTimestamp();
                Start = DateTime.Now;
            }
            System.Threading.Thread.Sleep(50);
        }

        Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());
    }

    
    private static void OutputSample(CounterSample s)
    {
        Console.WriteLine("\r\n+++++++++++");
        Console.WriteLine("Sample values - \r\n");
        Console.WriteLine("   BaseValue        = " + s.BaseValue);
        Console.WriteLine("   CounterFrequency = " + s.CounterFrequency);
        Console.WriteLine("   CounterTimeStamp = " + s.CounterTimeStamp);
        Console.WriteLine("   CounterType      = " + s.CounterType);
        Console.WriteLine("   RawValue         = " + s.RawValue);
        Console.WriteLine("   SystemFrequency  = " + s.SystemFrequency);
        Console.WriteLine("   TimeStamp        = " + s.TimeStamp);
        Console.WriteLine("   TimeStamp100nSec = " + s.TimeStamp100nSec);
        Console.WriteLine("++++++++++++++++++++++");
    }
}
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;

void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

void CollectSamples()
{
   String^ categoryName = "ElapsedTimeSampleCategory";
   String^ counterName = "ElapsedTimeSample";
   
   // Create the performance counter category.
   if (  !PerformanceCounterCategory::Exists( categoryName ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;
      
      // Add the counter.
      CounterCreationData^ ETimeData = gcnew CounterCreationData;
      ETimeData->CounterType = PerformanceCounterType::ElapsedTime;
      ETimeData->CounterName = counterName;
      CCDC->Add( ETimeData );
      
      // Create the category.
      PerformanceCounterCategory::Create( categoryName,
         "Demonstrates ElapsedTime performance counter usage.",
         CCDC );
   }
   else
   {
      Console::WriteLine( "Category exists - {0}", categoryName );
   }

   
   // Create the performance counter.
   PerformanceCounter^ PC = gcnew PerformanceCounter( categoryName,
                                                      counterName,
                                                      false );
   // Initialize the counter.
   PC->RawValue = Stopwatch::GetTimestamp();

   DateTime Start = DateTime::Now;
   
   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      // Output the values.
      if ( (j % 10) == 9 )
      {
         Console::WriteLine( "NextValue() = {0}", PC->NextValue() );
         Console::WriteLine( "Actual elapsed time = {0}", DateTime::Now.Subtract( Start ) );
         OutputSample( PC->NextSample() );
      }
      
      // Reset the counter on every 20th iteration.
      if ( j % 20 == 0 )
      {
         PC->RawValue = Stopwatch::GetTimestamp();
         Start = DateTime::Now;
      }
      System::Threading::Thread::Sleep( 50 );
   }

   Console::WriteLine( "Elapsed time = {0}", DateTime::Now.Subtract( Start ) );
}

int main()
{
   CollectSamples();
}
import System.*;
import System.Collections.*;
import System.Collections.Specialized.*;
import System.Diagnostics.*;
import System.Runtime.InteropServices.*;

public class App
{
    public static void main(String[] args)
    {
        CollectSamples();
    } //main

    public static void CollectSamples()
    {
        final String categoryName = "ElapsedTimeSampleCategory";
        final String counterName = "ElapsedTimeSample";

        if (!(PerformanceCounterCategory.Exists(categoryName))) {
            CounterCreationDataCollection ccdc = 
                new CounterCreationDataCollection();
            // Add the counter.
            CounterCreationData eTimeData = new CounterCreationData();
            eTimeData.set_CounterType(PerformanceCounterType.ElapsedTime);
            eTimeData.set_CounterName(counterName);
            ccdc.Add(eTimeData);
            // Create the category.
            PerformanceCounterCategory.Create(categoryName, 
                "Demonstrates ElapsedTime performance counter usage.", ccdc);
        }
        else {
            Console.WriteLine("Category exists - {0}", categoryName);
        }
        // Create the performance counter.
        PerformanceCounter pc = new PerformanceCounter(categoryName,
            counterName, false);
        // Initialize the counter.
        pc.set_RawValue(Stopwatch.GetTimestamp());
        DateTime start = DateTime.get_Now();
        // Loop for the samples.
        for (int j = 0; j < 100; j++) {
            // Output the values.
            if (j % 10 == 9) {
                Console.WriteLine("NextValue() = " + ((Single)pc.NextValue()).
                    ToString());
                Console.WriteLine("Actual elapsed time = "
                    + DateTime.get_Now().Subtract(start).ToString());
                OutputSample(pc.NextSample());
            }
            // Reset the counter on every 20th iteration.
            if (j % 20 == 0) {
                pc.set_RawValue(Stopwatch.GetTimestamp());
                start = DateTime.get_Now();
            }
            System.Threading.Thread.Sleep(50);
        }
        Console.WriteLine("Elapsed time = " + DateTime.get_Now().
            Subtract(start).ToString());
    } //CollectSamples

    private static void OutputSample(CounterSample s)
    {
        Console.WriteLine("\r\n+++++++++++");
        Console.WriteLine("Sample values - \r\n");
        Console.WriteLine("   BaseValue        = " + s.get_BaseValue());
        Console.WriteLine("   CounterFrequency = " + s.get_CounterFrequency());
        Console.WriteLine("   CounterTimeStamp = " + s.get_CounterTimeStamp());
        Console.WriteLine("   CounterType      = " + s.get_CounterType());
        Console.WriteLine("   RawValue         = " + s.get_RawValue());
        Console.WriteLine("   SystemFrequency  = " + s.get_SystemFrequency());
        Console.WriteLine("   TimeStamp        = " + s.get_TimeStamp());
        Console.WriteLine("   TimeStamp100nSec = " + s.get_TimeStamp100nSec());
        Console.WriteLine("++++++++++++++++++++++");
    } //OutputSample
} //App

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

PerformanceCounter 类
PerformanceCounter 成员
System.Diagnostics 命名空间