Home > Net >  Thread to sleep 10 ms, in a machine 15 ms
Thread to sleep 10 ms, in a machine 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 ();
}
}

CodePudding user response:

Threed Sleep only guarantee the interval is not less than 10, CPU scheduling, you also want to time, the computer itself is not a real-time system, the general accuracy in 100 ms

CodePudding user response:

Now is in the 2 machines can do 2 sets can't do that, because what I want to know

CodePudding user response:

reference 1st floor assky124 response:
Threed Sleep only guarantee the interval is not less than 10, CPU scheduling, you also want to time, the computer itself is not a real-time system, the general accuracy in 100 ms

And can't do but high processor large memory

CodePudding user response:

You said these three methods, which the execution result right?
The first: use the Sleep, still follows the two layers of circulation, look not to know what to do
The second: use the circulation and date function, date function accuracy is not high, more than ms level, another cycle will CPU time, affect the result will be
The third: use cycle and TickCount cycle will CPU time, affect the result will be

Windows itself is not high, the real-time requirement accurate to ms level is more difficult,

CodePudding user response:

Code too much and didn't finish, but I firmly believe that the code problem, do not believe that the computer precision or Windows itself,

CodePudding user response:

The result is normal, thread switching time slice at about 10 to 20 ms

CodePudding user response:

First you should know that Thread. Sleep is used to do, if you know that Sleep (0) and don't Sleep, the difference between this question answered,

CodePudding user response:

Thread.sleep () method of the Sleep time is not accurate, including the Timer (.net under two namespaces are the timing is not precise timing, writing code if you must use accurate timing, need to use other implementations (search "c # precise timing"), but, after careful analysis demand usually found that does not need precise timing, don't even need a Timer.
  •  Tags:  
  • C#
  • Related