这是片段:prog1:
HANDLE hM;
hM = CreateMutexA(NULL,TRUE, "abc"); // I have to use TRUE otherwise WaitForSingleObject fails.. by design it wants to take ownership and w//o TRUE there is no owner anyway right? <<-- **check this please if its true**
...
编2:
HANDLE hM;
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
while(WaitForSingleObject(hM,INFINITE)) {
// do smthing
ReleaseMutex(hM);
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc"); // In order to continue while loop but it doesnt rewind,
}
问题>它不会形成半无限循环......它进入一次就可以了。
目的?为了监控某些进程的活动(如果它崩溃我重新启动它),无限循环正在严重打击我的 CPU,我不能使用睡眠,我需要重新启动它 asasp(如果可以通过 while(1) 或 for( ;;) 请张贴)
更新
应该是 while(WaitForSingleObject(hM,INFINITE) == WAIT_ABANDONED){ ... },我没有正确复制:)。
它工作一次,然后它不会再等待,有没有办法重置句柄?
编1:
HANDLE hM;
hM = CreateMutexA(NULL,TRUE, "abc");
编2:
HANDLE hM;
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
while(WaitForSingleObject(hM,INFINITE)) {
ReleaseMutex(hM);
Sleep(1000);
CloseHandle(hM)
// do smthing like CreateProcess()
Sleep(1000);
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
}
如果有人想在这篇文章上添加评论,这样它就可以工作。