Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Wait for another program to finish

I have a program which calls a batch file several times to convert files on my computer.  The batch file can only process one file at a time, so I need a way for my program to wait until the other program has finished, then send the next file.  I have looked into using shellExecuteEx and waitForSingleObject, but am having trouble i think because it is a batch file and runs from dos.  All help is appreciated

-Scott
0
barry769
Asked:
barry769
1 Solution
 
DarthNemesisCommented:
I haven't tried this myself, but perhaps this answer will provide you a DOS prompt that pauses program flow until the batch processing is completed:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20550279.html
0
 
jkrCommented:
Use the following code:

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}

If you do not have a GUI, you can shorten it to

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   WaitForSingleObject     (     pi.hProcess, INFINITE);

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}
0
 
barry769Author Commented:
ok, sorry I am unfamiliar with c++, where do I put this code, where do I insert the file that is to be called, and where do I put the additional paramaters
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
jkrCommented:
Um, the answer pretty much hits the nail on the head, IMHO :o)
0
 
tinchosCommented:
Hi jkr

I won't argue that the answer is pretty fine.

As you would have noticed I'm just starting to moderate, and maybe I lack experience in it, but the fact that I recommend PAQ/Refund is that I consider that your comment is really helpful, but evidently (based on his last comment), it was not enough for the asker.

I'll try to explain what i consider an answer.....

Suppose I ask a question and I receive several comments for it.

If I have a correct answer, but I don't undestand how to implement it, or I have some doubt about it, I don't consider it an answer, instead, I place a comment asking the doubts I still have.

If my doubts are not answered, then I won't consider it an answer, not because the answer was not clear or because it was incorrect, but because I need more help for it to be an answer

Thats why I didn't believe this was an answer, simply because the asker hadn't solved his problem because he still needed help, just that.

English is not my native language, so I hope I'm making myself clear and you understand what I tried to explain.


PS: What is IMHO?
0
 
jkrCommented:
>>PS: What is IMHO?

"In My Humble Opinion" :o)

>>Thats why I didn't believe this was an answer, simply because the asker hadn't solved his
>>problem because he still needed help, just that.

You are right about that, but this situation seems to me a bit like

Q: "How do I drive to the train station with a car?"
A: "<explanation>"
Q: "OK, so could you teach me to drive?"
0
 
tinchosCommented:
I quite agree with you,

but I guess you never know if the asker's intention is to solve a problem without effort or if he really don't know how.
I guess I'd rather believe the latter of them and that's why my recommendation was PAQ/Refund.

Anyway, I recognize that your point of view is also understandable.
0
 
jkrCommented:
>>that's why my recommendation was PAQ/Refund

That's OK with me, then :o)
0
 
Computer101Commented:
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now