block main thread and allow secondary to run first
-
I have an external application that launches my application. I have a global variable that determines whether my app was launched via the other app or just by opening the executable. If launched via the other application, two processes or instances gets created and the global variable is set to 1. The first instance will be created but the window frame will not be shown. The other instance will display the window. One instance has the global value initialized to 0 while the other one has it set to 1. I need to block the instance that has global variable = 0 and let the other one run. Once that thread completes, I want the other thread (I believe it’s the main thread) to continue running. I tried using a mutex around my code but it still seems to be doing context-switching. How can I put a hold on thread1? I’ve tried using events but no such luck. This is what I have. Thanks!
InitInstance()
{
mutex = ::CreateMutex(NULL, FALSE, “X”);
if(mutex==NULL)
{
AfxMessageBox("Error: Problem with creating the mutex");
return FALSE;
}
else {
WaitForSingleObject(mutex, INFINITE);
if (!bGlobalVariable)
{
OneInstance = ::CreateMutex(NULL, FALSE, “instance”);
DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_ALREADY_EXISTS)
return FALSE;
}
ReleaseMutex(mutex);
}if (!GlobalVariable) //show window return TRUE;
}
-- modifed at 17:51 Thursday 25th August, 2005