当前位置:首页 > C#

C# 非独占延时函数 非Sleep

admin10年前 (2016-11-14)C#6816

在C#窗口程序中,如果在主线程里调用Sleep,在Sleep完成之前, 界面呈现出假死状态,不能响应任何操作!

下边实现的是非独占性延时函数,延时过时中界面仍可响应消息:

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

关于Math.Abs():

  Environment.TickCount,内部API是用DWORD GetTickCount()来实现的,该属性的值从系统计时器派生,并以 32 位有符号整数的形式存储。因此,如果系统连续运行,TickCount 将在约 24.9 天内从零递增至 Int32. MaxValue ,然后跳至 Int32. MinValue (这是一个负数),再在接下来的 24.9 天内递增至零。DWORD是无符号的,而 Environment.TickCount属性返回的值是有符号的,所以有一半的值用负数表示!


扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:https://feelsight.cn/post/25.html

“C# 非独占延时函数 非Sleep” 的相关文章

c#md5与SHA1验证函数

/// <summary>         /// MD5验证函数         /// &…

C#图片处理示例(裁剪,缩放,清晰度,水印)

using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using…

C#全局监听Windows键盘事件

1.工具类代码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Fo…

c#程序闪退日志记录/异常日志

以下代码是程序入口文件 using DDS_Form1; using System; using System.Collections.Generic; using System.IO; //using System.Linq; using System.Windows.Forms;…

C# .net动态加载第三方DLL

C#动态加载第三方DLL…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。