perfcounter.cs
来自「功能:基于windows mobile 的地图查看器。使用vs2005开发」· CS 代码 · 共 72 行
CS
72 行
using System;
using System.Runtime.InteropServices;
namespace PInvokeLibrary
{
/// <summary>
/// Provides access to the Performance Counters.
/// </summary>
public class PerfCounter
{
/// <summary>
/// This function retrieves the frequency of the high-resolution performance
/// counter if one is provided by the OEM.
/// </summary>
/// <param name="lpFrequency">Pointer to a variable that the function sets,
/// in counts per second, to the current performance-counter frequency. If
/// the installed hardware does not support a high-resolution performance
/// counter, this parameter can be set to zero.</param>
/// <returns>Nonzero indicates that the installed hardware supports a high-
/// resolution performance counter. Zero indicates that the installed hardware
/// does not support a high-resolution performance counter.</returns>
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceFrequency(ref Int64 lpFrequency);
/// <summary>
/// This function retrieves the current value of the high-resolution performance
/// counter if one is provided by the OEM.
/// </summary>
/// <param name="lpPerformanceCount">Pointer to a variable that the function sets,
/// in counts, to the current performance-counter value. If the installed hardware
/// does not support a high-resolution performance counter, this parameter can be
/// set to zero.</param>
/// <returns>QueryPerformanceFrequency will return 1000 if the hardware does not
/// support a high frequency counter since the API defaults to a milliseconds
/// GetTickCount implementation.</returns>
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceCounter(ref Int64 lpPerformanceCount);
/// <summary>
/// Run a test of the PerfCounter class.
/// </summary>
/// <param name="showLine">Delegate called to show debug information</param>
public static void TestProc(MainTest.DisplayLineDelegate showLine)
{
System.Int64 freq = 0;
if (QueryPerformanceFrequency(ref freq) != 0)
{
System.Int64 count1 = 0;
System.Int64 count2 = 0;
if (QueryPerformanceCounter(ref count1) != 0)
{
System.Threading.Thread.Sleep(1200);
QueryPerformanceCounter(ref count2);
showLine(String.Format("Timer Start: {0}", count1));
showLine(String.Format("Timer Stop: {0}", count2));
showLine(String.Format("Timer Freq: {0} Hz", freq));
showLine(String.Format("Slept for {0} ms", (count2 - count1) * 1000 / freq));
}
else
{
showLine("FAILURE: QueryPerformanceCounter failed");
}
}
else
{
showLine("FAILURE: QueryPerformanceFrequency failed");
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?