C#延时函数的使用说明

C#延时函数的使用说明

目录

C#延时函数使用

C#3个延时函数 

C#延时函数使用

在线程中如果需要延时,尽量不要使用Sleep()函数,这样会导致时间片切到别的线程中。

使用如下函数: 

    //Delay function     public static void Delay(int milliSecond)     {         int start = Environment.TickCount;         while (Math.Abs(Environment.TickCount - start) < milliSecond)         {             Application.DoEvents();          }     }

或者:

        //Delay us   Create a waitable timer         [DllImport("kernel32.dll")]         public static extern int CreateWaitableTimer(int lpTimerAttributes,                                      bool bManualReset, int lpTimerName);         public static void UsDelay(int us)         {             long duetime = -10 * us;             int hWaitTimer = CreateWaitableTimer(NULL, true, NULL);             SetWaitableTimer(hWaitTimer, ref duetime, 0, NULL, NULL, false);             while (MsgWaitForMultipleObjects(1, ref hWaitTimer, false, Timeout.Infinite,                      QS_TIMER)) ;             CloseHandle(hWaitTimer);         } C#3个延时函数  public static void Delays(int DelayTime = 100) { int time = Environment.TickCount; while (true) { if (Environment.TickCount - time >= DelayTime) { break; } Application.DoEvents(); Thread.Sleep(10); } } public static void Delay1(int milliSecond) { int start = Environment.TickCount; while (Math.Abs(Environment.TickCount - start) < milliSecond) { Application.DoEvents(); } } //延时程序 秒 public static bool Delay2(int delayTime) { DateTime now = DateTime.Now; int s; do { TimeSpan spand = DateTime.Now - now; s = spand.Seconds; Application.DoEvents(); } while (s < delayTime); return true; }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易知道(ezd.cc)。

推荐阅读

    excel怎么用乘法函数

    excel怎么用乘法函数,乘法,函数,哪个,excel乘法函数怎么用?1、首先用鼠标选中要计算的单元格。2、然后选中单元格后点击左上方工具栏的fx公

    excel中乘法函数是什么?

    excel中乘法函数是什么?,乘法,函数,什么,打开表格,在C1单元格中输入“=A1*B1”乘法公式。以此类推到多个单元。1、A1*B1=C1的Excel乘法公式

    标准差excel用什么函数?

    标准差excel用什么函数?,函数,标准,什么,在数据单元格的下方输入l标准差公式函数公式“=STDEVPA(C2:C6)”。按下回车,求出标准公差值。详细

    excel常用函数都有哪些?

    excel常用函数都有哪些?,函数,哪些,常用,1、SUM函数:SUM函数的作用是求和。函数公式为=sum()例如:统计一个单元格区域:=sum(A1:A10)  统计多个

    设置线程名称|tomcat线程名称设置

    设置线程名称|tomcat线程名称设置,,1. tomcat线程名称设置一.tomcat的优化1.tomcat的自身调优采用动静分离调优Tomcat线程池调优Tomcat的

    多线程cpu电脑|多线程的CPU

    多线程cpu电脑|多线程的CPU,,1. 多线程的CPU四核心四线程,表示这个电脑的CPU核心是4个核心、4个线程的。电脑CPU的核心数量和线程数量越多,