Benutzer:MovGP0/Performance/Counters

aus Wikipedia, der freien Enzyklopädie
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Performance Counter

Getting Current Value

var cpuCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName);

// getting the current value
float value = cpuCounter.NextValue();

Setting a value

Counter Value
var counter = new PerformanceCounter()
   {
      CategoryName = "MyCategory",
      CounterName = "Counter Name",
      MachineName = ".",
      ReadOnly = false
   };

counter.Increment();
counter.IncrementBy(value);

counter.Decrement();
counter.DecrementBy(value);

counter.RawValue(value);
AverageBase
  • Increment for every operation
AverageTimer32
// use this instead of System.DateTime.Now.Ticks
// to get a better measurement
[DllImport("Kernel32.dll")]
public static extern void QueryPerformanceCounter(ref long ticks);
PerformanceCounterSample counter = new PerformanceCounterSample();

QueryPerformanceCounter(ref startTime);

// do something

QueryPerformanceCounter(ref endTime);

counter.DoSomeProcessing(endTime - startTime);

Create a custom Counter

const string CategoryName = "PerfCountersTypringSpeed";

if(!PerformanceCounterCategory.Exists(CategoryName))
{
   var counterDataCollection = new CounterCreationDataCollection();

   var wpmCounter = new CounterCreationData
   {
      CounterType = PerformanceCounterType.RateOfCountsPerSecond32, 
      CounterName = "WordsPerMinute"
   };
   
   counterDataCollecction.Add(wpmCounter);

   try
   {
      PerformanceCounterCategory.Create(CategoryName, "Demo category to show how to create and consume counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);
   }
   catch(SecurityException)
   {
      // user has no permission to create counter
   }
}


Getting Admin Permissions

Manifest[1]
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
   <security>
     <requestedPrivileges>
        <requestedExecutionLevel level="highestAvailable" />
        <!-- or 
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
        -->
     </requestedPrivileges>
   </security>
</trustInfo>
</assembly>
Attribute
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]

or

[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, XML="...")]

with XML value

<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\">
   <Permission class=\"System.Security.Permissions.UIPermission, mscorlib\" version=\"1\">
      <AllWindows/>
   </Permission>
   <Permission class=\"System.Security.Permissions.RegistryPermission, mscorlib\" version=\"1\">
      <Unrestricted/>
   </Permission>
</PermissionSet>

or

[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, File = "pset.xml")]

References

  1. Step 6: Create and Embed an Application Manifest (UAC). In: MSDN. Microsoft, abgerufen am 4. August 2014.