桌面中有一个托盘程序,里面开启了一个线程,在线程里创建了新桌面,新桌面里面开了一个全屏程序,用WaitForSingleObject(pi.hProcess, INFINITE);等待全屏程序结束后,恢复到原桌面,
问题是:有时候会出现低概率的切换桌面不成功,全屏程序结束后,呈现出来的还是新桌面,既是全屏的桌面背景,没有恢复到原有桌面,这样的概率很低,但是存在,不知道为什么
STARTUPINFO si;
PROCESS_INFORMATION pi;
// Zero these structs
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.lpTitle = szDesktopName;
si.lpDesktop = szDesktopName;
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = m_hRead;
si.hStdOutput = m_hWrite;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
ZeroMemory(&pi, sizeof(pi));
// Start the child process
if (!CreateProcess(NULL, // No module name (use command line).
szPath, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to FALSE.
INHERIT_PARENT_AFFINITY, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
return FALSE;
}
while (!SetThreadDesktop(hNewDesktop))
{
Sleep(50);
}
while (!SwitchDesktop(hNewDesktop))
{
Sleep(50);
}
// Wait until process exits
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// Restore original ...
while (!SwitchDesktop(hOriginalInput))
{
Sleep(50);
}
while (!SetThreadDesktop(hOriginalThread))
{
Sleep(50);
}