Waiting for ShellExecute before going on
-
hello again, I need to do 3 steps on a file, I've written 3 functions ( step1(),step2(),step3()), all of those steps requires the execution of
ShellExecuteEx
but I need them do be done sequentialy with mutual exclusion. I've created a semaphore usingCreateSemaphore
but step2() starts before step1() finished. Here's the code//GLOBAL Variables HANDLE hSemaphore=NULL;
....Semaphore=CreateSemaphore(NULL, 1, 1, NULL); if(Semaphore ==NULL) { MessageBox(hwndMain, "Unable to assure mutual execution,can't continue","MUTEX",MB_OK|MB_ICONERROR ); break; }
//into step1()//we try to enter into critical region dwWaitResult=WaitForSingleObject(Semaphore,0L); //if(ShellExecute(hConsole,"open","encode_no_stego", Parameter, WorkingDir, SW_MAXIMIZE)==false) switch(dwWaitResult) { case WAIT_OBJECT_0: if(ShellExecuteEx(&sei)== NULL) { ReleaseSemaphore (Semaphore,1, NULL); return false; } ReleaseSemaphore (Semaphore,1, NULL); return true; }
//Into step2()dwWaitResult=WaitForSingleObject(Semaphore,INFINITE); while (dwWaitResult==WAIT_TIMEOUT) { };
If anyone can help me, that would be great, thanks! Paolo -
hello again, I need to do 3 steps on a file, I've written 3 functions ( step1(),step2(),step3()), all of those steps requires the execution of
ShellExecuteEx
but I need them do be done sequentialy with mutual exclusion. I've created a semaphore usingCreateSemaphore
but step2() starts before step1() finished. Here's the code//GLOBAL Variables HANDLE hSemaphore=NULL;
....Semaphore=CreateSemaphore(NULL, 1, 1, NULL); if(Semaphore ==NULL) { MessageBox(hwndMain, "Unable to assure mutual execution,can't continue","MUTEX",MB_OK|MB_ICONERROR ); break; }
//into step1()//we try to enter into critical region dwWaitResult=WaitForSingleObject(Semaphore,0L); //if(ShellExecute(hConsole,"open","encode_no_stego", Parameter, WorkingDir, SW_MAXIMIZE)==false) switch(dwWaitResult) { case WAIT_OBJECT_0: if(ShellExecuteEx(&sei)== NULL) { ReleaseSemaphore (Semaphore,1, NULL); return false; } ReleaseSemaphore (Semaphore,1, NULL); return true; }
//Into step2()dwWaitResult=WaitForSingleObject(Semaphore,INFINITE); while (dwWaitResult==WAIT_TIMEOUT) { };
If anyone can help me, that would be great, thanks! PaoloI take it you mean each step is running on its own thread? Semaphores dont help you gaurentee order. All the do is protect say thread A accessing the same thing as thread B. Which order thread A and thread B run in is down to the scheduler. So it might happen A grabs the semaphore, B tries to and waits, A releases semaphore, B contnues to run. Or it might happen B grabs the semaphore, A tries to and waits, B releases semaphore, A contnues to run - its up to the scheduler (and of course your theads priority). If you want things to happen one after the other, run them on the same thread: void Foo( { Step1(); Step2(); Step3(); } etc. If you really want each step to happen on it's own thread, then you need to use events. Look up CreateEvent/SetEvent/ResetEvent If I have the wrong end of the stick, and what you are asking is you want ShellExecuteEx not to return until the spawned process has finished, you do it like this. SHELLEXECUTEINFO si; si.fMask = SEE_MASK_NOCLOSEPROCESS; si.etc etc ShellExecuteEx(&si); WaitForSingleObject(si.hProcess, INFINATE);