登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

应用程序执行时间监测器  

2006-01-20 10:34:24|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

//应用程序执行时间监测器-IntervalTimer

---------------------------------------------

空间限制,删除了注释!!

---------------------------------------------

using System;

using System.Runtime.InteropServices;

 

namespace ymz.IntervalTimer

{

    public class IntervalTimer

    {

        #region API调用

        [DllImport("kernel32.dll")]

        static extern private int QueryPerformanceCounter(out long counter);

        [DllImport("kernel32.dll")]

        static extern private int QueryPerformanceFrequency(out long counter);

        #endregion

 

        #region 内部变量

        private TimerState state;

        private long ticksAtStart;

        private long intervalTicks;

        private static long frequency;

        private static int decimalPlaces;

        private static string formatString;

        private static bool initialized = false;

        #endregion

       

        public IntervalTimer()

        {

            if(! initialized)

            {

                QueryPerformanceFrequency(out frequency);

                decimalPlaces = (int)Math.Log10(frequency);

                // 根据频率的精确度设置当前平台的小数位数

                formatString = String.Format("interval : {{0:F{0}}} seconds ({{1}}ticks)",decimalPlaces);

                initialized = true;

            }

            state = TimerState.NotStarted;

        }

 

        public void Start()

        {

            state = TimerState.Started;

            QueryPerformanceCounter(out ticksAtStart);

        }

 

        public void Stop()

        {

            long current;

            QueryPerformanceCounter(out current);

            intervalTicks = current - ticksAtStart;

            state = TimerState.Stopped;

        }

 

        public float GetSeconds()

        {

            if(state != TimerState.Stopped)

                throw new TimerNotStoppedException();

            return (float)intervalTicks / (float)frequency;

        }

 

        public long GetIntervalTicks()

        {

            if(state != TimerState.Stopped)

                throw new TimerNotStoppedException();

            return intervalTicks;

        }

 

        public override string ToString()

        {

            if(state != TimerState.Stopped)

                return "Interval timer, state : " + state.ToString();

            else

                return String.Format(formatString, GetSeconds(), intervalTicks);

        }

 

    }

   

    public enum TimerState

    {

        NotStarted,

        Stopped,

        Started

    }

 

    public class TimerNotStoppedException : System.ApplicationException

    {

        public TimerNotStoppedException()

            : base("Timer is either still running or has not been started.")

        {

        }

    }

}

 

  评论这张
 
阅读(874)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018