+ -
当前位置:首页 → 问答吧 → GetTickCount函数为啥总是返回0 ?

GetTickCount函数为啥总是返回0 ?

时间:2011-12-13

来源:互联网

直接贴代码:
C/C++ code

void CframeDlg::OnBnClickedButtonFrame()
{
      m_Time_Start=GetTickCount();

    CDC* pDC=GetDlgItem(IDC_PLAYERXCTRL1)->GetWindowDC();
    HDC  m_hDC=pDC->GetSafeHdc();
    Screen2IplImage(m_hDC,"E://监控录像//xiaowenchao.bmp",m_Width,m_Height);
    ReleaseDC(pDC);
    DrawPic2hDC(m_Frame, IDC_STATIC_SHOWIMAGE);

    for(int i=0;i<10000;i++){}

    m_Time_End=GetTickCount();
    CString time_consumed;
    m_Time_Consumed=m_Time_End-m_Time_Start;
    time_consumed.Format("%d",m_Time_Consumed);
    MessageBox(time_consumed);
}


其中
C/C++ code

public:
    DWORD m_Time_Start;
public:
    DWORD m_Time_End;
public:
    DWORD m_Time_Consumed;


已经定义为成员变量,且 m_Time_Start=GetTickCount();已经在oninitialdlg()中初始化,m_Time_End初始化为0;
但是MessageBox()每次弹出0(经常是这个值)或者15、16。
请大家帮忙~

作者: xiaowenchaojlu   发布时间: 2011-12-13

论坛对此问题解释如下:

What might be happening is that your program runs too fast for it to update, due to its limited resolution.

You can debug the problem by putting a call to Sleep before the second GetTickCount, to see if the difference is now not 0. 

补充一下,GetTickCount并不准确,建议改用timeGetTime。
C/C++ code
DWORD timeGetTime(void);

作者: fight_in_dl   发布时间: 2011-12-13

m_Time_Start=GetTickCount();

m_Time_End=GetTickCount();
之间耗时太少,正常的
加大循环延时就看出效果了
for(int i=0;i<10000000;i++){}
在我机器上 才看出效果来

作者: gameslq   发布时间: 2011-12-13