Home > Net >  C # serial port every 10 ms, change machine after 15 ms
C # serial port every 10 ms, change machine after 15 ms

Time:10-24

Use several threads delay method, but the compiled program on other machines according to sleep time 15 ms, on its own machine is 10 ms
Int TimeMS=10;
While (SendDataTS_End)
{
While (SendDataTS_End)
{
A 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 & gt;
=(ms){
Pd=false;
}
}
}


[DllImport (" kernel32. DLL)]
The static extern uint GetTickCount ();
The static void Delay (uint ms)
{
Uint start=GetTickCount ();
While (GetTickCount () - start & lt; Ms)
{
//Application. DoEvents ();
}
}
  •  Tags:  
  • C#
  • Related