I have set pause to microsecund. I have Microsoft Development Environment
2002 (ver.7.0.9466) and .NET Framework 1.0 (ver 1.0.3705) and
Stopwatch stopWatch = new Stopwatch(); don't work
Your question is not very clear. But I will assume that you want to
pause for a number of microseconds.
The standard Thread.Sleep only takes milliseconds not microseconds.
But Socket.Select takes microseconds.
You should note that both have a semantic as "sleep minimum X" not
"sleep X".
When you go to sleep then other processes/threads start running. And
they may continue for a while after the sleeping period has expired.
The more busy the system is the longer the actual sleeping period will
be.
Furthermore there are some overhead and for very small sleep times
the overhead becomes significant.
The specifics will depend a lot on the computer you are using.
Here are some results from a randomly picked computer (mine !):
USleep 10 microseconds, overhead factor=97,703125
USleep 50 microseconds, overhead factor=19,53125
USleep 100 microseconds, overhead factor=9,765625
USleep 500 microseconds, overhead factor=1,953125
USleep 1000 microseconds, overhead factor=1,953125
Sleep 1 milliseconds, overhead factor=1,953125
Sleep 5 milliseconds, overhead factor=1,171875
Sleep 10 milliseconds, overhead factor=1,0625
Sleep 50 milliseconds, overhead factor=1,015625
Sleep 100 milliseconds, overhead factor=1,015625
Sleep 500 milliseconds, overhead factor=1
Sleep 1000 milliseconds, overhead factor=1
(program attached below)
The short version is that Sleep(1 millisecond) actually takes
2 milliseconds at average, but that USleep(10 microseconds)
actually takes 1000 microseconds at average.
You will have to evaluate what is useful for you.
Arne
PS: VS 2002 / .NET 1.0 is extremely old - upgrade is overdue !
====================================
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace E
{
public static class Util
{
private static Socket s;
static Util()
{
s = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
s.Bind(new IPEndPoint(IPAddress.Any, 12345));
}
public static void USleep(int us)
{
IList sl = new ArrayList();
sl.Add(s);
Socket.Select(sl, null, null, us);
}
}
public class Program
{
public static void TestUSleep(int us)
{
DateTime t1 = DateTime.Now;
for(int i = 0; i < 1000000/us; i++)
{
Util.USleep(us);
}
DateTime t2 = DateTime.Now;
Console.WriteLine("USleep " + us + " microseconds, overhead
factor=" + (t2 - t1).TotalSeconds);
}
public static void TestSleep(int ms)
{
DateTime t1 = DateTime.Now;
for(int i = 0; i < 1000/ms; i++)
{
Thread.Sleep(ms);
}
DateTime t2 = DateTime.Now;
Console.WriteLine("Sleep " + ms + " milliseconds, overhead
factor=" + (t2 - t1).TotalSeconds);
}
public static void Main(string[] args)
{
TestUSleep(10);
TestUSleep(50);
TestUSleep(100);
TestUSleep(500);
TestUSleep(1000);
TestSleep(1);
TestSleep(5);
TestSleep(10);
TestSleep(50);
TestSleep(100);
TestSleep(500);
TestSleep(1000);
Console.ReadKey();
}
}
}