?
Solved

Wait for another program to finish

Posted on 2003-03-14
10
Medium Priority
?
225 Views
Last Modified: 2010-04-01
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
Comment
Question by:barry769
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8136867
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
 
LVL 86

Expert Comment

by:jkr
ID: 8136879
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
 

Author Comment

by:barry769
ID: 8136994
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.

 
LVL 9

Expert Comment

by:tinchos
ID: 9491888
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
 
LVL 86

Expert Comment

by:jkr
ID: 9491909
Um, the answer pretty much hits the nail on the head, IMHO :o)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9491988
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
 
LVL 86

Expert Comment

by:jkr
ID: 9492036
>>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
 
LVL 9

Expert Comment

by:tinchos
ID: 9492090
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
 
LVL 86

Expert Comment

by:jkr
ID: 9492131
>>that's why my recommendation was PAQ/Refund

That's OK with me, then :o)
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9537702
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question