C# 超級延時,高精度延時,窗口程序不卡死延時


C# 超級延時,高精度延時,窗口程序不卡死延時

 

一:封裝延時函數

 

        /// <summary>
        /// 高精度延時,窗口程序不卡死延時
        /// </summary>
        /// <param name="time">1000微秒 = 1毫秒 ; 1000毫秒 = 1秒</param>
        /// <param name="type">可空:毫秒  0:毫秒  1:微秒  2:秒  3:分  4:小時  5:天</param>
        public static void SuperSleep(int time, int type = 0)
        {
            if (time < 1)
            {
                return;
            }

            int hTimer = 0;
            long Interval = 0;
            int i = 0;

            int INFINITE = -1;
            int QS_ALLINPUT = 255;
            int WAIT_OBJECT_0 = 0;

            if (type == 1)
            {
                Interval = -10 * time;
                hTimer = CreateWaitableTimer(0, true, "WaitableTimer");
                SetWaitableTimer(hTimer, ref Interval, 0, 0, 0, false);

                while (MsgWaitForMultipleObjects(1,ref hTimer, false, INFINITE, QS_ALLINPUT) != WAIT_OBJECT_0)
                {
                    System.Windows.Forms.Application.DoEvents();
                }

                CloseHandle(hTimer);
                return;
            }
            if (type == 0)
            {
                type = 1;
            }
            if (type == 2)
            {
                type = 1000;
            }
            if (type == 3)
            {
                type = 1000 * 60;
            }
            if (type == 4)
            {
                type = 1000 * 60 * 60;
            }
            if (type == 5)
            {
                type = 1000 * 60 * 60 * 24;
            }

            Interval = -10 * time * 1000 * type;
            hTimer = CreateWaitableTimer(0, true, "WaitableTimer");
            SetWaitableTimer(hTimer, ref Interval, 0, 0, 0, false);
            while (MsgWaitForMultipleObjects(1,ref hTimer, false, INFINITE, QS_ALLINPUT) != WAIT_OBJECT_0)
            {
                System.Windows.Forms.Application.DoEvents();
            }
            CloseHandle(hTimer);
        }

 

 

二:定義winapi

        /// <summary>
        /// 創建或打開一個可等待的計時器對象
        /// </summary>
        /// <param name="lpTimerAttributes"></param>
        /// <param name="bManualReset"></param>
        /// <param name="lpTimerName"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern int CreateWaitableTimer(int lpTimerAttributes, bool bManualReset, string lpTimerName);

        /// <summary>
        /// 激活指定的等待計時器
        /// </summary>
        /// <param name="hTimer"></param>
        /// <param name="ft"></param>
        /// <param name="lPeriod"></param>
        /// <param name="pfnCompletionRoutine"></param>
        /// <param name="pArgToCompletionRoutine"></param>
        /// <param name="fResume"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        static extern bool SetWaitableTimer(int hTimer, [In] ref long pDueTime, int lPeriod, int pfnCompletionRoutine, int pArgToCompletionRoutine, bool fResume);

        /// <summary>
        /// 等待直到一個或所有指定對象處於信號狀態或超時間隔過去
        /// </summary>
        /// <param name="nCount"></param>
        /// <param name="pHandles"></param>
        /// <param name="fWaitAll"></param>
        /// <param name="dwMilliseconds"></param>
        /// <param name="dwWakeMask"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern int MsgWaitForMultipleObjects(int nCount, ref int pHandles, bool fWaitAll, int dwMilliseconds, int dwWakeMask);

        /// <summary>
        /// 關閉打開的對象句柄。
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern int CloseHandle(int hObject);

 

 Ps:使用此延時不會造成窗口或其他程序造成假死的狀態,等待時間到后可以繼續執行后面的代碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM