首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

WaitForSingleObject()函数有关问题

2012-02-12 
WaitForSingleObject()函数问题我想要进行一段设置操作,这个操作有一部分少用的操作总是在重复,我想利用Wa

WaitForSingleObject()函数问题
我想要进行一段设置操作,这个操作有一部分少用的操作总是在重复,我想利用WaitForSingleObject()函数来设置一个时间段,这段时间内如果再进行这个设置操作,就不执行这个少用的操作部分,等待超时的时候再执行这段代码
所以我要问的是:WaitForSingleObject()的第一个参数句柄应该设置成什么事件的句柄,如何来操作这些,最好有代码,谢谢!

[解决办法]

C/C++ code
HANDLE hMutex; // Create a mutex with no initial owner.hMutex = CreateMutex(     NULL,                       // default security attributes    FALSE,                      // initially not owned    NULL);                      // unnamed mutexBOOL FunctionToWriteToDatabase(HANDLE hMutex) {     DWORD dwWaitResult;     // Request ownership of mutex.     dwWaitResult = WaitForSingleObject(         hMutex,   // handle to mutex        5000L);   // five-second time-out interval     switch (dwWaitResult)     {        // The thread got mutex ownership.        case WAIT_OBJECT_0:             __try {                 // Write to the database.            }             __finally {                 // Release ownership of the mutex object.                if (! ReleaseMutex(hMutex))                 {                     // Deal with error.                }                 break;             }         // Cannot get mutex ownership due to time-out.        case WAIT_TIMEOUT:             return FALSE;         // Got ownership of the abandoned mutex object.        case WAIT_ABANDONED:             return FALSE;     }    return TRUE; } 

热点排行