/*
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <process.h>
#include <stdio.h>
unsigned __stdcall TF(void* arg) {
HANDLE timer=(HANDLE) arg;
while (1) {
//此处,进程间通信的接收方
//timer是命名的,因此进程间或线程间没有区别
WaitForSingleObject(timer,INFINITE);
printf(".");
}
}
int main(int argc, char* argv[]) {
//创建,命名为0,也可以是LPCTSTR,字符串
//其他进程可以通过OpenWaitableTimer获得此timer的句柄,并对之进行SetWaitableTimer
HANDLE timer = CreateWaitableTimer(
0,
false, // false=>will be automatically reset
0); // name
LARGE_INTEGER li;
const int unitsPerSecond=10*1000*1000; // 100 nano seconds
// Set the event the first time 2 seconds
// after calling SetWaitableTimer
//2秒
li.QuadPart=-(2*unitsPerSecond);
//通过句柄设置timer
SetWaitableTimer(
timer,
&li,
750, // Set the event every 750 milli Seconds
0,
0,
false);
//用TF函数启动worker线程
_beginthreadex(0,0,TF,(void*) timer,0,0);
// Wait forever,
while (1) ;
return 0;
}
*/
#define _WIN32_WINNT 0x0400
//#include <windows.h>
#include <stdio.h>
#include <afxwin.h>
#define _SECOND 10000000
typedef struct _MYDATA {
TCHAR *szText;
DWORD dwValue;
} MYDATA;
VOID CALLBACK TimerAPCProc(
LPVOID lpArg, // Data value.
DWORD dwTimerLowValue, // Timer low value.
DWORD dwTimerHighValue ) { // Timer high value.
MYDATA *pMyData = (MYDATA *)lpArg;
printf( "Message: %s"nValue: %d"n"n", pMyData->szText,
pMyData->dwValue );
MessageBeep(0);
}
int main( void ) {
/*
HANDLE hTimer;
BOOL bSuccess;
__int64 qwDueTime;
LARGE_INTEGER liDueTime;
MYDATA MyData;
TCHAR szError[255];
MyData.szText = "This is my data.";
MyData.dwValue = 100;
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 5 seconds from now.
qwDueTime = -5 * _SECOND;
// 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.
2000, // Periodic timer interval of 2 seconds.
TimerAPCProc, // Completion routine.
&MyData, // Argument to the completion routine.
FALSE ); // Do not restore a suspended system.
if ( bSuccess ) {
for ( ; MyData.dwValue < 1000; MyData.dwValue += 100 ) {
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 );
}
*/
//创建定时器QQ:9073204
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
//设置相对时间为10秒。
liDueTime.QuadPart = -100000000;
//创建定时器。
//NULL:是定时器的属性,为默认值;
//True表示是否手动复位,
//最后一个参数是定时器的名字
hTimer = CreateWaitableTimer(NULL, FALSE, _T("TestWaitableTimer"));
if (!hTimer)
{
return 1;
}
//OutputDebugString(_T("10秒定时器"r"n"));
//printf( "Message: %s"nValue: %d"n"n", pMyData->szText,pMyData->dwValue );
printf("10秒定时器"r"n");
// 设置10秒钟。
//定时器的句柄
//定时器开始的时间,为负数就是相对时间,从执行这条语句开始多少秒触发定时器
//定时器的周期
//回调函数
//回调函数的参数
//设置系统是否自动恢复
if (!SetWaitableTimer(hTimer, &liDueTime, 10000, NULL, NULL, 0))
{
//
CloseHandle(hTimer);
return 2;
}
//等定时器有信号。
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
{
//OutputDebugString(_T("10秒定时器出错了"r"n"));
//
printf("10秒定时器出错了"r"n");
CloseHandle(hTimer);
return 3;
}
else
{
while(1)
{
WaitForSingleObject(hTimer, INFINITE);
//10秒钟到达。
//OutputDebugString(_T("10秒定时器到了"r"n"));
printf("10秒定时器到了"r"n");
}
}
//
CloseHandle(hTimer);
return 0;
}