zoukankan      html  css  js  c++  java
  • 【共享】WinCE 6.0中精确定时的实现

    借用了网友关于win32平台精确定时的方法,将平台调用中的kernel32.dll换成coredll.dll即可。经测试,时间精度到ms以下

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace SmartDeviceProject3
    {
    public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();
    }

    private void MainForm_Paint(object sender, PaintEventArgs e)
    {
    int sum=1;
    HiPerfTimer pt
    = new HiPerfTimer(); // 创建新的 HiPerfTimer 对象
    pt.Start(); // 启动计时器
    for (int i = 1; i < 100000; i++)
    {
    sum
    *= i;
    }
    pt.Stop();
    // 停止计时器
    MessageBox.Show(pt.Duration.ToString());//执行时间按2.35ms,SBC8100开发板
    }
    class HiPerfTimer
    {
    [DllImport(
    "coredll.dll")]
    private static extern bool QueryPerformanceCounter(
    out long lpPerformanceCount);

    [DllImport(
    "coredll.dll")]
    private static extern bool QueryPerformanceFrequency(
    out long lpFrequency);

    private long startTime, stopTime;
    private long freq;

    // 构造函数
    public HiPerfTimer()
    {
    startTime
    = 0;
    stopTime
    = 0;

    if (QueryPerformanceFrequency(out freq) == false)
    {
    // 不支持高性能计数器
    throw new System.Exception("High performance timer not supported");
    }
    }

    // 开始计时器
    public void Start()
    {
    // 来让等待线程工作
    System.Threading.Thread.Sleep(0);

    QueryPerformanceCounter(
    out startTime);
    }

    // 停止计时器
    public void Stop()
    {
    QueryPerformanceCounter(
    out stopTime);
    }

    // 返回计时器经过时间(单位:毫秒)
    public double Duration
    {
    get
    {
    return ((double)(stopTime - startTime) / (double)freq) * 1000;
    }
    }
    }
    }
  • 相关阅读:
    (转载)什么才是富人思维
    linux上的vs code的C++环境搭建
    [转载]双线性插值简介
    刻意练习行动手册
    滑动窗口技巧
    [转载]用于深入思考的小工具
    CF632E Thief in a Shop
    BZOJ1497 最大获利
    UVA10779 Collectors Problem
    洛谷P4311 士兵占领
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/1998399.html
Copyright © 2011-2022 走看看