110,552
社区成员
发帖
与我相关
我的任务
分享
private delegate void TimeProc(uint id, uint msg, int user, int dw1, int dw2);
[DllImport("Winmm.dll")]
private extern static int timeSetEvent(uint delay, uint resolution, TimeProc timeProc, int user, uint fuEvent);
public void fangfa1()
{
//xxx.....
timeSetEvent(1, 1,new TimeProc(fangfa2), 0, 0);
}
public void fangfa2()
{
fangfa3();
}
public void fangfa3()
{
//xxx.....
}
public static void Test()
{
Console.WriteLine( Stopwatch.Frequency ); // 输出 CPU 频率
Stopwatch sw = new Stopwatch( );
sw.Start( );
int s = 0;
for( int i = 0; i < 60000; ++i ) // 测试 Stopwatch 精度
s += i;
sw.Stop( );
Console.WriteLine( sw.ElapsedMilliseconds );
Console.WriteLine( sw.ElapsedTicks );
sw.Reset( );
sw.Start( );
Thread.Sleep( 0 ); // 看看 Sleep(0) 花了多少时间
sw.Stop( );
Console.WriteLine( sw.ElapsedMilliseconds );
Console.WriteLine( sw.ElapsedTicks );
sw.Reset( );
sw.Start( );
Thread.Sleep( 1 ); // 看看 Sleep(1) 花了多少时间
sw.Stop( );
Console.WriteLine( sw.ElapsedMilliseconds );
Console.WriteLine( sw.ElapsedTicks );
sw.Reset( );
sw.Start( );
while( true ) // 使用 Sleep(0) 进行精度控制
{
if( sw.ElapsedMilliseconds >= 1 )
break;
Thread.Sleep( 0 );
}
sw.Stop( );
Console.WriteLine( sw.ElapsedMilliseconds );
Console.WriteLine( sw.ElapsedTicks );
}