免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3528 | 回复: 4
打印 上一主题 下一主题

[C++] VC++ 显示毫秒级的时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-03-20 20:25 |只看该作者 |倒序浏览
VC++ 显示毫秒级的时间,的时分秒后,继续显示微秒/毫秒,了解一下毫秒级的定时器,请关注最后的代码:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <sys/timeb.h>
#include <time.h>
UINT ShowTimeProc(LPVOID lParam);        //定时器函数
BOOL CMilliSecondDlg::OnInitDialog()
{
        CDialog::OnInitDialog();
        //开启定时器
AfxBeginThread(ShowTimeProc,this);
        return TRUE;
}
void CMilliSecondDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
                CAboutDlg dlgAbout;
                dlgAbout.DoModal();
        }
        else
        {
                CDialog::OnSysCommand(nID, lParam);
        }
}
// http://www.codesc.net
HCURSOR CMilliSecondDlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}
UINT ShowTimeProc(LPVOID lParam)
{
        CMilliSecondDlg* pDlg = (CMilliSecondDlg*)lParam;
        DWORD dwStart, dwStop;
        // 起始值和终止值
dwStop = GetTickCount();
        while(TRUE)
        {
                // 上一次的终止值变成新的起始值
dwStart = dwStop;
                //发送消息通知对话框该更新时间了
::SendMessage(pDlg->m_hWnd,WM_UPDATETIME,0,0);
                do
                {
                        dwStop = GetTickCount();
                }while(dwStop-50<dwStart);
        }
        return 0;
}
//更新时间
void CMilliSecondDlg::OnUpdateTime()
{
        struct _timeb timebuffer;
        char *timeline;
//获得毫秒级的时间
_ftime( &timebuffer );
        timeline = ctime(&(timebuffer.time));
        //格式化时间
m_strTime.Format("当前时间是:%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
        UpdateData(FALSE);
}

论坛徽章:
0
2 [报告]
发表于 2018-03-22 10:58 |只看该作者
你那个while(TRUE)和do while会把CPU占满的。
linux下比较简单, gettimeofday就可以了。
windows最好用QueryPerformanceCounter记录起止时间, 然后线程里循环的时候可以用select定时,也可以waitforsinglethread。绝不能直接while(TRUE)

论坛徽章:
0
3 [报告]
发表于 2018-03-22 11:02 |只看该作者
live555和pjsip里都有实现,下面是live的:


#if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
// For Windoze, we need to implement our own gettimeofday()

// used to make sure that static variables in gettimeofday() aren't initialized simultaneously by multiple threads
static LONG initializeLock_gettimeofday = 0;  

#if !defined(_WIN32_WCE)
#include <sys/timeb.h>
#endif

int gettimeofday(struct timeval* tp, int* /*tz*/) {
  static LARGE_INTEGER tickFrequency, epochOffset;

  static Boolean isInitialized = False;

  LARGE_INTEGER tickNow;

#if !defined(_WIN32_WCE)
  QueryPerformanceCounter(&tickNow);
#else
  tickNow.QuadPart = GetTickCount();
#endif

  if (!isInitialized) {
    if(1 == InterlockedIncrement(&initializeLock_gettimeofday)) {
#if !defined(_WIN32_WCE)
      // For our first call, use "ftime()", so that we get a time with a proper epoch.
      // For subsequent calls, use "QueryPerformanceCount()", because it's more fine-grain.
      struct timeb tb;
      ftime(&tb);
      tp->tv_sec = tb.time;
      tp->tv_usec = 1000*tb.millitm;

      // Also get our counter frequency:
      QueryPerformanceFrequency(&tickFrequency);
#else
      /* FILETIME of Jan 1 1970 00:00:00. */
      const LONGLONG epoch = 116444736000000000LL;
      FILETIME fileTime;
      LARGE_INTEGER time;
      GetSystemTimeAsFileTime(&fileTime);

      time.HighPart = fileTime.dwHighDateTime;
      time.LowPart = fileTime.dwLowDateTime;

      // convert to from 100ns time to unix timestamp in seconds, 1000*1000*10
      tp->tv_sec = (long)((time.QuadPart - epoch) / 10000000L);

      /*
        GetSystemTimeAsFileTime has just a seconds resolution,
        thats why wince-version of gettimeofday is not 100% accurate, usec accuracy would be calculated like this:
        // convert 100 nanoseconds to usec
        tp->tv_usec= (long)((time.QuadPart - epoch)%10000000L) / 10L;
      */
      tp->tv_usec = 0;

      // resolution of GetTickCounter() is always milliseconds
      tickFrequency.QuadPart = 1000;
#endif     
      // compute an offset to add to subsequent counter times, so we get a proper epoch:
      epochOffset.QuadPart
          = tp->tv_sec * tickFrequency.QuadPart + (tp->tv_usec * tickFrequency.QuadPart) / 1000000L - tickNow.QuadPart;
      
      // next caller can use ticks for time calculation
      isInitialized = True;
      return 0;
    } else {
        InterlockedDecrement(&initializeLock_gettimeofday);
        // wait until first caller has initialized static values
        while(!isInitialized){
          Sleep(1);
        }
    }
  }

  // adjust our tick count so that we get a proper epoch:
  tickNow.QuadPart += epochOffset.QuadPart;

  tp->tv_sec =  (long)(tickNow.QuadPart / tickFrequency.QuadPart);
  tp->tv_usec = (long)(((tickNow.QuadPart % tickFrequency.QuadPart) * 1000000L) / tickFrequency.QuadPart);

  return 0;
}
#endif

论坛徽章:
0
4 [报告]
发表于 2018-03-22 11:03 |只看该作者
本帖最后由 sxcong 于 2018-03-22 11:04 编辑

论坛出错,重复了

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
5 [报告]
发表于 2018-03-25 18:49 |只看该作者
shanshuiboy 发表于 2018-03-20 20:25
VC++ 显示毫秒级的时间,的时分秒后,继续显示微秒/毫秒,了解一下毫秒级的定时器,请关注最后的代码:

#ifde ...

WINDOWS达不到毫秒级精度,大约是6~8毫秒的分辨率。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP