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:使用此延时不会造成窗口或其他程序造成假死的状态,等待时间到后可以继续执行后面的代码。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义