线程同步(计时器)

来源:互联网 发布:淘宝上的海外专营店 编辑:程序博客网 时间:2024/06/11 08:53

1.CreateWaitableTimer()函数创建等待计时器。
2.SetWaitableTimer()函数设置创建的计时器时间间隔。
3.CancelWaitableTimer()函数取消计时器。

#include <stdio.h>#include <stdlib.h>#include <windows.h>#define WIN32_WINNT 0x500#define ONE_SECOND 10000000typedef struct _APC_PROC_ARG{    TCHAR *szText;    DWORD dwValue;}APC_PROC_ARG;VOID CALLBACK TimerAPCProc(LPVOID lpArg,DWORD dwTimerLowValue,DWORD dwTimerHighValue){    APC_PROC_ARG *pApcData=(APC_PROC_ARG *)lpArg;    printf("Message :%s\n Value :%d\n\n",pApcData->szText,pApcData->dwValue);    MessageBeep(MB_OK);}void main(void){    HANDLE hTimer;    BOOL bSuccess;    INT64 qwDueTime;    LARGE_INTEGER liDueTime;    APC_PROC_ARG ApcData;    ApcData.szText="message to apc proc.";    ApcData.dwValue=1;    hTimer=CreateWaitableTimer(NULL,FALSE,"MyTimer");    if(!hTimer)    {        printf("CreateWaitableTimer failed with Error %d.",GetLastError());        return;    }    else    {        _try        {            qwDueTime=-5*ONE_SECOND;            liDueTime.LowPart=(DWORD)(qwDueTime & 0xFFFFFFFF);            liDueTime.HighPart=(LONG)(qwDueTime >> 32);            bSuccess=SetWaitableTimer(hTimer,&liDueTime,2000,TimerAPCProc,&ApcData,FALSE);            if(bSuccess)            {                for(;ApcData.dwValue<10;ApcData.dwValue++)                {                    SleepEx(INFINITE,TRUE);                }            }            else            {                printf("SetWaitableTimer failed with Error %d.",GetLastError());            }        }        _finally        {            CloseHandle(hTimer);        }    }}
0 0
原创粉丝点击