您的位置:程序门 -> delphi -> windows sdk/api



如何获取线程的执行时间?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何获取线程的执行时间?[已结贴,结贴人:lunyx]
发表于:2007-11-04 19:41:04 楼主
新建了一个线程,如何在线程结束后获取它的执行时间?
发表于:2007-11-04 19:48:241楼 得分:10
var      
            ct,et,kt,ut:_filetime;      
            systime:_systemtime;      
    begin      
            getthreadtimes(new.handle,ct,et,kt,ut);      
            filetimetosystemtime(ct,systime);      
            memo1.lines.add('create       '+format('%2d-%2d-%2d       %2d:%2d:%2d',[systime.wyear,systime.wmonth,systime.wday,systime.whour,systime.wminute,systime.wsecond]));      
       
    注:systime得到的是格林威治时间
发表于:2007-11-04 20:53:052楼 得分:0
用getthreadtimes方法是可以得到线程的开始时间的,可是结束时间得不到啊,老是得到“1601-1-1   0:   0:   0   ”
,难道直接用土方法,在线程建立和结束时分别记录当时的时间到变量中?
发表于:2007-11-04 20:59:403楼 得分:10
在线程建立和结束时分别记录当时的时间到变量中
发表于:2007-11-04 22:20:484楼 得分:10
在线程create的事件里记录时间值,在线程结束的finally里块里再纪录一个时间。通过比较这两个值来获得。

或者你建立结构:
tthreadparam   =   packed   record
    start_time:   tdatetime;   //开始时间
    end_time:   tdatetime;   //结束时间
end;

在create时由主线程调用后写入start_time,然后在线程的ontermilate事件里再取一次时间,写入到时间结构里end_time,相减后就得到了执行的时间差了。
发表于:2007-11-06 15:58:155楼 得分:5
applebomb  

的方法不错。
发表于:2007-11-13 14:32:216楼 得分:5
在线程建立和结束时分别记录当时的时间到变量中
发表于:2007-11-13 16:04:207楼 得分:5
都说了  
发表于:2007-11-14 08:44:368楼 得分:20
正如上面所说的,当线程开始的时候记录一下时间,在线程结束之后,再次记录,两次相减就可以得到时间差。
如果时间差很小的话,可以考虑用浮点的形式记录,如:
format('%9.8f',[timenow2-timenow1])//timenow2和timenow1分别表示两次的时间记录
发表于:2007-11-15 13:08:289楼 得分:15
getthreadtimes就可以,

lpexittime

points   to   a   filetime   structure   that   receives   the   exit   time   of   the   thread.   if   the   thread   has   not   exited,   the   content   of   this   structure   is   undefined.  
发表于:2007-11-15 15:06:5510楼 得分:20
c# code
function filetimetodatetime(const afiletime: tfiletime): tdatetime; var vsystemtime: tsystemtime; vlocalfiletime: tfiletime; begin filetimetolocalfiletime(afiletime, vlocalfiletime); filetimetosystemtime(vlocalfiletime, vsystemtime); result := systemtimetodatetime(vsystemtime); end; { filetimetodatetime } procedure tform1.button1click(sender: tobject); var vcreationtime, vexittime, vkerneltime, vusertime: tfiletime; begin getthreadtimes(getcurrentthread, vcreationtime, vexittime, vkerneltime, vusertime); memo1.lines.add(datetimetostr(filetimetodatetime(vcreationtime))); memo1.lines.add(inttostr(int64(vexittime) * 100 div 1000000000)); //单位为100纳秒,一纳秒=十亿分之一秒 memo1.lines.add(inttostr(int64(vkerneltime) * 100 div 1000000000)); memo1.lines.add(inttostr(int64(vusertime) * 100 div 1000000000)); end;


快速检索

最新资讯
热门点击