只运行1个实例判断,任务管理器没有,程序却提示存在???
ac_de 2010-07-19 11:05:38
我用信号量和窗口增加标记拌饭 来判断是否 已经运行了1个实例,
bool CTest1::IfProgramExited()
{
HANDLE hSem = CreateSemaphore(NULL, 1, 1,"ProgramA");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hSem);
HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(),
GW_CHILD);
while (::IsWindow(hWndPrevious))
{
if (::GetProp(hWndPrevious, “test"))
{
if (::IsIconic(hWndPrevious))
::ShowWindow(hWndPrevious,
SW_RESTORE);
::SetForegroundWindow(hWndPrevious);
::SetForegroundWindow(
::GetLastActivePopup(hWndPrevious));
return TRUE;
}
// 继续寻找下一个窗口
hWndPrevious = ::GetWindow(hWndPrevious,
GW_HWNDNEXT);
}
if (::IsIconic(hWndPrevious))
:ShowWindow(hWndPrevious, SW_RESTORE);
// 将主窗激活
::SetForegroundWindow(hWndPrevious);
// 将主窗的对话框激活
::SetForegroundWindow(
::GetLastActivePopup(hWndPrevious));
return true;
}
return false;
}
CTestApp::InitInstance()
{
//设置标记
SetProp(m_pMainWnd->GetSafeHwnd(),"test","");
}
//去掉标记
CMainFrame::OnDestroy()
{
RemoveProp(m_hWnd,"test");
}
但是有的时候 程序退出后,在任务管理器中,以及各没有了test.exe进程,但是用上述函数判断的时候,却还提示
“程序已经存在”,请问是什么原因???? 谢谢