Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Waiting for ShellExecute before going on

Waiting for ShellExecute before going on

Scheduled Pinned Locked Moved C / C++ / MFC
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paolo Ponzano
    wrote on last edited by
    #1

    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 using CreateSemaphore 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

    D 1 Reply Last reply
    0
    • P Paolo Ponzano

      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 using CreateSemaphore 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

      D Offline
      D Offline
      Diddy
      wrote on last edited by
      #2

      I 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);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups