📄 perfcounter.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -