以下代码段
[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
public static extern uint TimeBeginPeriod(uint uMilliseconds);
static void Main(string[] args)
{
if (TimeBeginPeriod(1) != 0)
Console.WriteLine("TimeBeginPeriod failed!");
Console.WriteLine("Sleep");
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1);
Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
sw.Restart();
}
Console.WriteLine("Threading.Timer");
sw = null;
System.Threading.Timer t = null;
int n = 0;
t = new Timer(state =>
{
if (sw == null)
sw = Stopwatch.StartNew();
else
{
Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
n++;
sw.Restart();
} …Run Code Online (Sandbox Code Playgroud)