7 个解决方案
#1
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
// Wait for the timer.
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
}
例子是10秒,自己改改
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
// Wait for the timer.
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
}
例子是10秒,自己改改
#2
up
#3
这个和sleep()一样,10毫秒还可以,1毫秒就误差太大
#4
使用sleep函数的不利只处在于期间不能处理其他的消息,如果时间太长,就好象死机一样,利用ColeDateTime类和ColeDateTimeSpan类实现延时:
ColeDateTime start_time = ColeDateTime::GetCurrentTime();
ColeDateTimeSpan end_time = ColeDateTime::GetCurrentTime()-start_time;
While(end_time.GetTotalSeconds() <= 2)
{
MSG msg;
GetMessage(&msg,NULL,0,0);
PreTranslateMessage(&msg);
End_time = ColeDateTime::GetCurrentTime-start_time;
}
这样在延时的时候我们也能够处理其他的消息。
ColeDateTime start_time = ColeDateTime::GetCurrentTime();
ColeDateTimeSpan end_time = ColeDateTime::GetCurrentTime()-start_time;
While(end_time.GetTotalSeconds() <= 2)
{
MSG msg;
GetMessage(&msg,NULL,0,0);
PreTranslateMessage(&msg);
End_time = ColeDateTime::GetCurrentTime-start_time;
}
这样在延时的时候我们也能够处理其他的消息。
#5
There is no way to achieve accurate delay in Windows, because it's not a real time operating system.
Try use a dual core CPU.
Try use a dual core CPU.
#6
要求太高,做不到
#7
可以用Multimedia Timer
timeBeginPeriod(1); //设精度为1ms
timeSetEvent(1, 1, TimeCallBack, (DWORD)m_hWnd, TIME_PERIODIC); //设置timer
在TimeCallBack中向窗口送WM_TIMER消息即可
timeBeginPeriod(1); //设精度为1ms
timeSetEvent(1, 1, TimeCallBack, (DWORD)m_hWnd, TIME_PERIODIC); //设置timer
在TimeCallBack中向窗口送WM_TIMER消息即可
#1
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
// Wait for the timer.
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
}
例子是10秒,自己改改
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
// Wait for the timer.
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
}
例子是10秒,自己改改
#2
up
#3
这个和sleep()一样,10毫秒还可以,1毫秒就误差太大
#4
使用sleep函数的不利只处在于期间不能处理其他的消息,如果时间太长,就好象死机一样,利用ColeDateTime类和ColeDateTimeSpan类实现延时:
ColeDateTime start_time = ColeDateTime::GetCurrentTime();
ColeDateTimeSpan end_time = ColeDateTime::GetCurrentTime()-start_time;
While(end_time.GetTotalSeconds() <= 2)
{
MSG msg;
GetMessage(&msg,NULL,0,0);
PreTranslateMessage(&msg);
End_time = ColeDateTime::GetCurrentTime-start_time;
}
这样在延时的时候我们也能够处理其他的消息。
ColeDateTime start_time = ColeDateTime::GetCurrentTime();
ColeDateTimeSpan end_time = ColeDateTime::GetCurrentTime()-start_time;
While(end_time.GetTotalSeconds() <= 2)
{
MSG msg;
GetMessage(&msg,NULL,0,0);
PreTranslateMessage(&msg);
End_time = ColeDateTime::GetCurrentTime-start_time;
}
这样在延时的时候我们也能够处理其他的消息。
#5
There is no way to achieve accurate delay in Windows, because it's not a real time operating system.
Try use a dual core CPU.
Try use a dual core CPU.
#6
要求太高,做不到
#7
可以用Multimedia Timer
timeBeginPeriod(1); //设精度为1ms
timeSetEvent(1, 1, TimeCallBack, (DWORD)m_hWnd, TIME_PERIODIC); //设置timer
在TimeCallBack中向窗口送WM_TIMER消息即可
timeBeginPeriod(1); //设精度为1ms
timeSetEvent(1, 1, TimeCallBack, (DWORD)m_hWnd, TIME_PERIODIC); //设置timer
在TimeCallBack中向窗口送WM_TIMER消息即可