国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

可精確到1毫秒的用以在記錄文件中寫入自定義的調(diào)試信息(主要是時間)的組件

2024-07-21 02:24:17
字體:
供稿:網(wǎng)友
菜鳥學(xué)堂:
上次我發(fā)布了一個用以在記錄文件中寫入自定義的調(diào)試信息(主要是時間)的組件,但由于clr的限制,它只能精確到10毫秒左右。后來我參考了網(wǎng)絡(luò)上的一篇文章:http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx(在這里首先向原作者表示感謝)通過調(diào)用系統(tǒng)api得到了可精確到1毫秒左右的時間記錄。故特重新用c#重寫了這個組件,與大家共享。

//====================================================================
//tracespy - 用以在記錄文件中寫入自定義的調(diào)試信息(開發(fā)者:林健)
//====================================================================
//
//屬性:
//       tracefilename        - 記錄文件名
//
//方法:
//   ★文本寫入方面
//       writetext            - 寫入自定義文本
//       clearalltext         - 清除所有文本
//   ★時間記錄方面
//       settimepoint         - 設(shè)置時間起點
//       gettimespanfrominit  - 詢問時間跨度(距離時間起點)
//       gettimespanfromprev  - 詢問時間跨度(距離上次詢問時間)
//   ★自定義計數(shù)器
//       setcounter           - 設(shè)置自定義計數(shù)器
//       addcounter           - 累加自定義計數(shù)器
//
//====================================================================

using system;

namespace tracespy
{
    public class thetrace
    {

        //記錄文件名
        static public string tracefilename = "trace.txt";

        //時間起點(初始為當(dāng)前時刻)
        static private long inittimepoint = timecounter.getexactnow().ticks;

        //上次詢問時間點(初始為當(dāng)前時刻)
        static private long prevtimepoint = timecounter.getexactnow().ticks;

        //自定義計數(shù)器
        static private int counter = 0;

        //寫入自定義文本
        static public void writetext(string str)
        {
            writetext(str, false);
        }

        static public void writetext(string str, bool showtime)
        {
            filewriter.writetext(str, showtime);
        }

        //清除所有文本
        static public void clearalltext()
        {
             filewriter.clearalltext();
        }

        //設(shè)置時間起點
        static public void settimepoint()
        {
             settimepoint("");
        }

        static public void settimepoint(string note)
        {
            inittimepoint = timecounter.getexactnow().ticks;
            prevtimepoint = timecounter.getexactnow().ticks;
            filewriter.writetext("設(shè)置時間起點[" + note + "]。", false);
        }

        //詢問時間跨度(距離時間起點)
        static public decimal gettimespanfrominit()
        {
            return gettimespanfrominit("");
        }

        static public decimal gettimespanfrominit(string note)
        {
            prevtimepoint = timecounter.getexactnow().ticks;
            decimal span;
            span = (decimal)(prevtimepoint - inittimepoint) / (decimal)10000;
            filewriter.writetext("詢問時間跨度[" + note + "],距離時間起點為" + span.tostring() + "毫秒。", false);
            return span;
        }

        //詢問時間跨度(距離上次詢問時間)
        static public decimal gettimespanfromprev()
        {
            return gettimespanfromprev("");
        }

        static public decimal gettimespanfromprev(string note)
        {
            long recttimepoint =timecounter.getexactnow().ticks;
            decimal span;
            span = (decimal)(recttimepoint - prevtimepoint) / (decimal)10000;
            prevtimepoint = recttimepoint;
            filewriter.writetext("詢問時間跨度[" + note + "],距離上次詢問時間為" + span.tostring() + "毫秒。", false);
            return span;
        }

        //設(shè)置自定義計數(shù)器
        static public int setcounter()
        {
            return setcounter(0);
        }
        static public int setcounter(int num)
        {
            counter = num;
            filewriter.writetext("自定義計數(shù)器值設(shè)置為" + counter + "。", false);
            return counter;
        }
        
        //累加自定義計數(shù)器
        static public int addcounter()
        {
            return addcounter(1);
        }
        static public int addcounter(int num)
        {
            counter += num;
            filewriter.writetext("自定義計數(shù)器值累加到" + counter + "。", false);
            return counter;
        }
    }
}

using system;
using system.runtime.interopservices;

namespace tracespy
{
    internal class timecounter
    {
        [dllimport("kernel32.dll")]
        static extern bool queryperformancecounter([in, out] ref long lpperformancecount);
        [dllimport("kernel32.dll")]
        static extern bool queryperformancefrequency([in, out] ref long lpfrequency);

        static long _f = 0;

        static private long gettickcount()
        {
            long f = _f;
            if (f == 0)
            {
                if (queryperformancefrequency(ref f))
                {
                    _f = f;
                }
                else
                {
                    _f = -1;
                }
            }
            if (f == -1)
            {
                return environment.tickcount * 10000;
            }
            long c = 0;
            queryperformancecounter(ref c);
            return (long)(((double)c) * 1000 * 10000 / ((double)f));
        }

        static long _tc = 0;

        static internal datetime getexactnow()
        {
            if (_tc == 0)
            {
                long tc = gettickcount();
                datetime dt = datetime.now;
                _tc = dt.ticks - tc;
                return dt;
            }
            return new datetime(_tc + gettickcount());
        }
    }
}

using system;

namespace tracespy
{
    internal class filewriter
    {
        static private system.io.streamwriter writer;

        //向文件中寫入一個字串
        static internal void writetext(string str, bool showtime)
        {
            if(thetrace.tracefilename == string.empty)
                return;
            writer = new system.io.streamwriter(thetrace.tracefilename, true, system.text.encoding.default);
            string words;
            words = str;
            if(showtime)
                words += " @ " + timecounter.getexactnow().tolongdatestring() + " " + timecounter.getexactnow().tolongtimestring();
            writer.writeline(words);
            writer.close();
        }

        //清除記錄文件
        static internal void clearalltext()
        {
            if(thetrace.tracefilename == string.empty)
                return;
            writer = new system.io.streamwriter(thetrace.tracefilename, false, system.text.encoding.default);
            writer.write("");
            writer.close();
        }
    }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 荣成市| 沿河| 县级市| 徐州市| 资源县| 禄丰县| 确山县| 金门县| 黎城县| 罗江县| 固原市| 通榆县| 寻乌县| 尼玛县| 合肥市| 新乡县| 长沙县| 康定县| 宾阳县| 铅山县| 通河县| 区。| 綦江县| 南澳县| 将乐县| 麦盖提县| 莫力| 铅山县| 金川县| 手机| 专栏| 西乌珠穆沁旗| 普陀区| 东源县| 河北省| 北安市| 高要市| 普安县| 奈曼旗| 砀山县| 定结县|