我在C#中使用线程遇到了一个小问题。奇怪的是,当我打开 Chrome 浏览器时,我的线程速度从32ms延迟变成16ms延迟;当我关闭Chrome浏览器时,它又恢复成32ms。我使用Thread.Sleep(1000 / 60)
进行延迟。有人能解释一下为什么会出现这种情况,并可能提供可能的解决方案吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static bool alive;
static Thread thread;
static DateTime last;
static void Main(string[] args)
{
alive = true;
thread = new Thread(new ThreadStart(Loop));
thread.Start();
Console.ReadKey();
}
static void Loop()
{
last = DateTime.Now;
while (alive)
{
DateTime current = DateTime.Now;
TimeSpan span = current - last;
last = current;
Console.WriteLine("{0}ms", span.Milliseconds);
Thread.Sleep(1000 / 60);
}
}
}
}