const int WAIT_OBJECT_0 = 0;
        const int QS_ALLINPUT = 255;
        const int INFINITE = -1;        //自定义数据类型
        struct LARGE_INTEGER
        {
            public long QuadPart;        }
[DllImport("kernel32.dll")]
        static extern bool SetWaitableTimer(int hTimer, 
                                            LARGE_INTEGER pDueTime,
                                            int lPeriod,
                                            int pfnCompletionRoutine,
                                            int lpArgToCompletionRoutine,
                                            bool fResume);
        [DllImport("kernel32.dll")]
        static extern int CreateWaitableTimerA(int lpTimerAttributes,
                                              bool bManualReset,
                                              int  lpTimerName);
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseHandle(int hObject);        [DllImport("user32.dll")]
        static extern int MsgWaitForMultipleObjects(int nCount, int pHandles,
           bool bWaitAll, int dwMilliseconds, int dwWakeMask);
//调用时SetWaitableTimer 那里开始出错...程序的作用是一个精确到毫秒的等待.不知不用API,C#里是否有自己带的涵数
        public void gjdwait(int weimiao)
        {
            int hTimer;
            LARGE_INTEGER int65=new LARGE_INTEGER ();
            
            int65.QuadPart = -10 * weimiao;
            hTimer =CreateWaitableTimerA (0,false ,0);
            SetWaitableTimer(hTimer, int65, 0, 0, 0, false);
            while (MsgWaitForMultipleObjects(1, hTimer, false, INFINITE, QS_ALLINPUT) != WAIT_OBJECT_0)
            {
                Application.DoEvents();
            }
            CloseHandle(hTimer);        }

解决方案 »

  1.   

    自带的DateTime有精确到毫秒的...
    不要什么都去用WinAPI
      

  2.   

    我说错了..是精确到 微秒  . 就是  等侍的时候程序不会 假死 或 长期占用CPU100%
      

  3.   

    开一个线程,直接 Thread.Sleep(毫秒);还用得着去写个什么函数
      

  4.   

    说得明确点,主线程定义一个分支线程 
    Thread thread1 = new Thread(new ThreadStart(方法名))
    指定执行你要进行的操作(方法),不要带参数然后再开一个 Timer 控件,设置 Timer 的 Interval 属性
    (毫秒级,隔多少时间执行一次 Tick 方法)
    定义好 Timer 的 Tick 方法(比如检查分支线程的执行状况啦,显示百分比啊)最后 thread1.Start()
         Timer.Start()
    OK。