2015-04-05 5 views
0

Я пытаюсь изменить PerformanceCounter, который я создал на C#. Но, похоже, это не меняется. Этот счетчик должен быть фактическим, чтобы быть флагом: 0 или 1.PerformanceCounter RawValue не изменен modified

Я взял следующий код из сети. Он создал коллекционную категорию вместе с счетчиками. Но RawValue всегда показывает 0!

Я работаю над Win7/64.

Можете ли вы посоветовать?

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 

namespace PerformanceCounterSample 
{ 
    /// <summary> 
    /// Entry class for the sample 
    /// </summary> 
    class PerformanceCounterSampleStarter 
    { 
     /// <summary> 
     /// Imports the <code>QueryPerformanceFrequency</code> me thod into the class. The method is used to measure the current 
     /// tickcount of the system. 
     /// </summary> 
     /// <param name="ticks">current tick count</param> 
     [DllImport("Kernel32.dll")] 
     public static extern void QueryPerformanceCounter(ref long ticks); 

     /// <summary> 
     /// Main thread 
     /// </summary> 
     [STAThread] 
     static void Main(string[] args) 
     { 
      PerformanceCounterSample test = new PerformanceCounterSample(); 
      Random rand = new Random(); 
      long startTime = 0; 
      long endTime = 0; 

      for (int i=0; i<1000; i++) 
      { 
       // measure starting time 
       QueryPerformanceCounter(ref startTime); 

       System.Threading.Thread.Sleep(rand.Next(500)); 

       // measure ending time 
       QueryPerformanceCounter(ref endTime); 

       // do some processing 
       test.DoSomeProcessing(endTime - startTime); 
      } 
     } 
    } 

    /// <summary> 
    /// The PerformanceCounterSample class sets up a performance counter category called "JamboCpmTool" if it does not already 
    /// exists and adds some counters to it. It provides a method to increment these counters. 
    /// </summary> 
    public class PerformanceCounterSample 
    { 
     private PerformanceCounter m_cntrJoinConctConf_VPG_MyJamboLogin; 

     public PerformanceCounterSample() 
     { 
      if (!PerformanceCounterCategory.Exists("JamboCpmTool")) 
      { 
       CounterCreationDataCollection counters = new CounterCreationDataCollection(); 

       CounterCreationData VPG_MyJamboLogin = new CounterCreationData(); 
       VPG_MyJamboLogin.CounterName = "JoinConnectConf_VPG_MyJamboLogin"; 
       VPG_MyJamboLogin.CounterHelp = "Join Connect Conf VPG MyJambo Login Success"; 
       VPG_MyJamboLogin.CounterType = PerformanceCounterType.NumberOfItems64; 
       counters.Add(VPG_MyJamboLogin); 

       // create new category with the counters above 
       System.Diagnostics.PerformanceCounterCategory.Create("JamboCpmTool", "Sample category for Codeproject", PerformanceCounterCategoryType.SingleInstance, counters); 
      } 

      // create counters to work with 
      m_cntrJoinConctConf_VPG_MyJamboLogin = new PerformanceCounter(); 
      m_cntrJoinConctConf_VPG_MyJamboLogin.CategoryName = "JamboCpmTool"; 
      m_cntrJoinConctConf_VPG_MyJamboLogin.CounterName = "JoinConctConf_VPG_MyJamboLogin"; 
      m_cntrJoinConctConf_VPG_MyJamboLogin.MachineName = "."; 
      m_cntrJoinConctConf_VPG_MyJamboLogin.ReadOnly = false; 
      m_cntrJoinConctConf_VPG_MyJamboLogin.RawValue = 0; 
     } 

     /// <summary> 
     /// Increments counters. 
     /// </summary> 
     /// <param name="ticks">The number of ticks the AverageTimer32 counter must be incremented by</param> 
     public void DoSomeProcessing(long ticks) 
     { 
      m_cntrJoinConctConf_VPG_MyJamboLogin.RawValue = 100L; 
      Console.WriteLine("m_cntrJoinConctConf_VPG_MyJamboLogin = " + m_cntrJoinConctConf_VPG_MyJamboLogin.RawValue); 
     } 
    } 
} 

ответ

0

Чтобы заполнить объект, вам необходимо позвонить m_cntrJoinConctConf_VPG_MyJamboLogin.NextValue();.

Примечание: первый раз, когда вы звоните NextValue() RawValue будет 0 как функция является сравнительным. Таким образом, в следующий раз, когда вы позвоните NextValue(), у вас должен быть ваш ответ.

Минимальная рекомендуемая частота между считывающими первичными счетчиками составляет 1 секунду.

Для получения более подробной информации см. Это MSDN article.

 Смежные вопросы

  • Нет связанных вопросов^_^