#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>#define _SECOND 10000000 //10000000 * 100ns / 1000000000 = 1sVOID CALLBACK TimerAPCProc( LPVOID lpArg, // Data value.
DWORD dwTimerLowValue, // Timer low value.
DWORD dwTimerHighValue ) { // Timer high value. DWORD dwTmp = GetTickCount();
printf( "Message: %s\nValue: %d\n\n", "The system tickcount is", dwTmp );
}
void main( void ) { HANDLE hTimer;
BOOL bSuccess;
__int64 qwDueTime;
LARGE_INTEGER liDueTime;
TCHAR szError[255]; if ( hTimer = CreateWaitableTimer( NULL, // Default security attributes.
FALSE, // Create auto-reset timer.
"MyTimer" ) )
{ // Name of waitable timer. __try
{ // Create a negative 64-bit integer that will be used to
// signal the timer 1 seconds from now.
qwDueTime = -1 * _SECOND; //qwDurTime 100ns // Copy the relative time into a LARGE_INTEGER.
liDueTime.LowPart = (DWORD) ( qwDueTime & 0xFFFFFFFF );
liDueTime.HighPart = (LONG) ( qwDueTime >> 32 );
bSuccess = SetWaitableTimer(hTimer, // Handle to the timer object.
&liDueTime, // When timer will become signaled.
500, // Periodic timer interval of 0.5 seconds.
//this min value is 1 ms
TimerAPCProc, // Completion routine.
NULL, // Argument to the completion routine.
FALSE ); // Do not restore a suspended system.
if ( bSuccess )
{
for (int a = 0 ; a < 10; a ++)
{
SleepEx(INFINITE, // Wait forever.
TRUE ); // IMPORTANT!!! The thread must be in an
// alertable state to process the APC.
}
}
else
{
wsprintf( szError, "SetWaitableTimer() failed with Error %d.", GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
} }
__finally
{
CloseHandle( hTimer );
} }
else
{
wsprintf( szError, "CreateWaitableTimer() failed with Error %d.",
GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
}
}
我要做一个定时器。我用UDP做的文件传输。所以在自己解丢包的问题,每发一个数据包就有一个确认包。当收不到确认包时。就要定时重发。经理要我做一个定时器类。这里SetWaitableTimer函数是计时的吗?为什么还要下面的那个别if判断?真的看不太明白。。帮忙谢。
#include <windows.h>
#include <stdio.h>#define _SECOND 10000000 //10000000 * 100ns / 1000000000 = 1sVOID CALLBACK TimerAPCProc( LPVOID lpArg, // Data value.
DWORD dwTimerLowValue, // Timer low value.
DWORD dwTimerHighValue ) { // Timer high value. DWORD dwTmp = GetTickCount();
printf( "Message: %s\nValue: %d\n\n", "The system tickcount is", dwTmp );
}
void main( void ) { HANDLE hTimer;
BOOL bSuccess;
__int64 qwDueTime;
LARGE_INTEGER liDueTime;
TCHAR szError[255]; if ( hTimer = CreateWaitableTimer( NULL, // Default security attributes.
FALSE, // Create auto-reset timer.
"MyTimer" ) )
{ // Name of waitable timer. __try
{ // Create a negative 64-bit integer that will be used to
// signal the timer 1 seconds from now.
qwDueTime = -1 * _SECOND; //qwDurTime 100ns // Copy the relative time into a LARGE_INTEGER.
liDueTime.LowPart = (DWORD) ( qwDueTime & 0xFFFFFFFF );
liDueTime.HighPart = (LONG) ( qwDueTime >> 32 );
bSuccess = SetWaitableTimer(hTimer, // Handle to the timer object.
&liDueTime, // When timer will become signaled.
500, // Periodic timer interval of 0.5 seconds.
//this min value is 1 ms
TimerAPCProc, // Completion routine.
NULL, // Argument to the completion routine.
FALSE ); // Do not restore a suspended system.
if ( bSuccess )
{
for (int a = 0 ; a < 10; a ++)
{
SleepEx(INFINITE, // Wait forever.
TRUE ); // IMPORTANT!!! The thread must be in an
// alertable state to process the APC.
}
}
else
{
wsprintf( szError, "SetWaitableTimer() failed with Error %d.", GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
} }
__finally
{
CloseHandle( hTimer );
} }
else
{
wsprintf( szError, "CreateWaitableTimer() failed with Error %d.",
GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
}
}
我要做一个定时器。我用UDP做的文件传输。所以在自己解丢包的问题,每发一个数据包就有一个确认包。当收不到确认包时。就要定时重发。经理要我做一个定时器类。这里SetWaitableTimer函数是计时的吗?为什么还要下面的那个别if判断?真的看不太明白。。帮忙谢。
Q184796 HOWTO: Use a Waitable Timer with an Asynchronous Procedure Call
http://support.microsoft.com/support/kb/articles/q184/7/96.asp