用了幾種執行緒延時的方法,但是編譯后的程式在別的機器上就顯示休眠時間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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/186718.html
標籤:C#
上一篇:rest wcf 如何傳參
