线程休眠10ms,换一台机器变15ms
用了几种线程延时的方法,但是编译后的程序在别的机器上就显示休眠时间15ms,在自己的机器上就是10ms
int TimeMS =10;
while (SendDataTS_End)
{
while (SendDataTS_End)
{
Stopwatch sw = new Stopwatch();
sw.Start();
//Delay(TimeMS);
//Wait(TimeMS);
System.Threading.Thread.Sleep(TimeMS);
//Thread.SpinWait(TimeMS);
//Thread.Sleep(TimeMS);
sw.Stop();
long totalTime = sw.ElapsedMilliseconds;
}
}
public static void Wait(int ms)
{
DateTime t = DateTime.Now;
bool pd = true;
while (pd)
{
TimeSpan ts = DateTime.Now - t;
if (ts.TotalMilliseconds >= ms)
{
pd = false;
}
}
}
[DllImport("kernel32.dll")]
static extern uint GetTickCount();
static void Delay(uint ms)
{
uint start = GetTickCount();
while (GetTickCount() - start < ms)
{
//Application.DoEvents();
}
}