c++定时事件控制台错误
C++ timing events console bug?
我正试图写一个类,将能够使用QueryPerformanceCounter在c++时间事件。这个想法是,你创建一个计时器对象,给一个函数一个双格式的时间,它计数直到时间过去,然后做一些事情。这个类最适合用于游戏中的计时(游戏邦注:例如,拥有一个每秒计数60次的计时器)。当我编译这段代码时,它只是向控制台输出0,似乎永远都是这样。但我注意到一些我无法理解的bug。如果我点击控制台窗口的滚动条并按住它,计时器就会正确计数。例如,如果我输入5.0,然后快速点击并按住滚动条5秒或更长时间,当我放手时,程序将打印"完成!!"那么,当我让它将经过的时间打印到控制台时,为什么它不能正确计数呢?打印东西到控制台是否有故障,或者我的定时代码是否有问题?下面是代码:
#include <iostream>
#include <iomanip>
#include "windows.h"
using namespace std;
int main()
{
setprecision(10); // i tried to see if precision in the stream was the problem but i don't think it is
cout << "hello! lets time something..." << endl;
bool timing = 0; // a switch to turn the timer on and off
LARGE_INTEGER T1, T2; // the timestamps to count
LARGE_INTEGER freq; // the frequency per seccond for measuring the difference between the stamp values
QueryPerformanceFrequency(&freq); // gets the frequency from the computer
// mil.QuadPart = freq.QuadPart / 1000; // not used
double ellapsedtime = 0, desiredtime; // enter a value to count up to in secconds
// if you entered 4.5 for example, then it should wait for 4.5 secconds
cout << "enter the amount of time you would like to wait for in seconds (in double format.)!!" << endl;
cin >> desiredtime;
QueryPerformanceCounter(&T1); // gets the first stamp value
timing = 1; // switches the timer on
while(timing)
{
QueryPerformanceCounter(&T2); // gets another stamp value
ellapsedtime += (T2.QuadPart - T1.QuadPart) / freq.QuadPart; // measures the difference between the two stamp
//values and then divides them by the frequency to get how many secconds has ellapsed
cout << ellapsedtime << endl;
T1.QuadPart = T2.QuadPart; // assigns the value of the second stamp to the first one, so that we can measure the
// difference between them again and again
if(ellapsedtime>=desiredtime) // checks if the elapsed time is bigger than or equal to the desired time,
// and if it is prints done and turns the timer off
{
cout << "done!!!" << endl;
timing = 0; // breaks the loop
}
}
return 0;
}
应该在ellapsedtime
中存储自第一次调用QueryPerformanceCounter
以来经过的微秒数,并且不应该覆盖第一个时间戳。
// gets another stamp value
QueryPerformanceCounter(&T2);
// measures the difference between the two stamp
ellapsedtime += (T2.QuadPart - T1.QuadPart);
cout << "number of tick " << ellapsedtime << endl;
ellapsedtime *= 1000000.;
ellapsedtime /= freq.QuadPart;
cout << "number of Microseconds " << ellapsedtime << endl;
// checks if the elapsed time is bigger than or equal to the desired time
if(ellapsedtime/1000000.>=desiredtime) {
cout << "done!!!" << endl;
timing = 0; // breaks the loop
}
请务必阅读:获取高分辨率时间戳
相关文章:
- C++出现控制台错误.我无法识别源代码的问题
- C++无法解决我的问题。我有控制台错误
- 在错误检查非 int 的内容时,控制台被淹没
- Visual Studio控制台调试了错误的文件
- C++ 中控制台代码运行时的分段错误
- 将函数从控制台应用程序移动到共享库项目似乎会带来不相关的编译错误
- 控制台在结束时崩溃(循环)未超出限制错误
- cout 和 cerr 的控制台输出顺序错误
- 控制台崩溃,尚无法识别错误
- Windows 控制台应用 (C++) 具有错误的退出代码
- 将第三方模块的控制台输出/错误重定向到日志文件
- C++:关闭控制台应用后出现异常错误
- C++控制台程序崩溃,没有任何错误
- 在VS2012静态库,控制台应用程序和clr程序上使用cryptopp的链接错误
- Seg 错误初始化列表向量与控制台输入
- C++附加控制台包含错误
- 我的控制台给我一个错误
- 分配控制台错误代码
- 制作了一个井字控制台应用程序,我有多个无法解决的错误
- 在导致错误控制台应用程序的库中使用MFC