Solved

Function that informs me when EXE is finished

Posted on 2000-05-03
22
214 Views
Last Modified: 2010-04-04
Hi Experts ;
I have a simple question.
In my Delphi program I run an external EXE File
(With Winexec command) that open a file ("success.doc")
and send data to this File.
After that code I wanna open this File and read the data.
the problem is that I open the file before the EXE finshed sending the
data, and therfor I am getting an eror.
I added a sleep(2000) command between the WinExec command and the openFile
command. This solution is not good enough since I can never be sure
that the milisec I gave to this process is enough, and it changes
from computer to computer. I am looking for a command that let me
run the EXE file and to let me know when it finished to run, only then
I'll open the data File (Success.doc) - I think it should be an API
function.

Here is my code :

winexec('C:\WIndows\SendRules.bat',SW_HIDE);
          sleep(3000);
          AssignFile(SuccessFile, 'C:\Windows\success.doc');
          Reset(SuccessFile);

Yaron
0
Comment
Question by:yaron_avraham
[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
  • 7
  • 4
  • 2
  • +8
22 Comments
 

Author Comment

by:yaron_avraham
ID: 2773005
Adjusted points from 50 to 60
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2773019
Hi Yaron,

what you need is to get access to the process handle to wait for the process. This is not possible with WInExec hence use this code from my PAQs:

function ExecuteProcessAndWait(CmdLine : String) : boolean;
var
  StartUpInfo : TStartupInfo;
begin
  Result := False;
  Fillchar(StartUpInfo, sizeof(StartUpInfo), 0);
  With StartUpInfo do
    begin
    cb := sizeof(StartUpInfo);
    dwflags := STARTF_USESHOWWINDOW;
    wShowWindow := SW_SHOWNORMAL;
    end;
  if CreateProcess(nil,
                   PChar(CmdLine),
                   nil,
                   nil,
                   false,
                   0,
                   nil,
                   nil,
                   StartUpInfo,
                   ProcessInfo) then
    begin
    WaitForSingleObject(ProcessInfo.hprocess, INFINITE)
    Result := True;
    end;
end;

Ciao, Mike
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2773144
listening....
0
Industry Leaders: 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!

 

Expert Comment

by:Grayl1
ID: 2773438
Hi!

Try using Application.ProcessMessages; in stead of Sleep(3000);
0
 
LVL 2

Expert Comment

by:NetoMan
ID: 2773612
listen...
0
 
LVL 1

Expert Comment

by:drnadeem
ID: 2773976
Listening
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2774658
listenning
0
 
LVL 5

Expert Comment

by:Gwena
ID: 2775143
I'm listening too :-)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2776337
Mike has the answer - but aaaaaaaaaa - please please guys, don't never ever forget to close that damned windows handles!!!

  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread );

Everytime someone posts such an ExecuteAndWait function I have to write this comment...   :-(   Perhaps I should write this comment's text into a textfile, then I can save some time in the next thread.   :-D

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2776679
Now you don't need any longer (at least if I post it :-). In my real code I close the handles correctly, but this double-whooper-with-cheese-all-in-one-execute-and-wait function simply has not made it through my control check yet, but now...

Ciao, Mike
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2776696
:-)  That's nice...  (-:
0
 

Expert Comment

by:johnstoned
ID: 2779939
Mike's answer will stop your program responding to windows.

If you change

WaitForSingleObject(ProcessInfo.hprocess, INFINITE)
   
to

while WaitForSingleObject(ProcessInfo.hProcess, 50) = WAIT_TIMEOUT do
  Application.ProcessMessages;

then the program will still respond to windows redraws and things.

Dave.
0
 
LVL 1

Accepted Solution

by:
saulite earned 60 total points
ID: 2787977
You could use the Windows standart program "start.exe"
instead of using:
      winexec('C:\WIndows\SendRules.bat',SW_HIDE);
use:
      winexec('start /w C:\WIndows\SendRules.bat',SW_HIDE);
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2787992
And another new expert! Welcome, saulite...   :-)

May I friendly request something? Here in the Delphi forum it is common practice to lock questions with an answer only if you are the first expert in the thread, or if all other comments are evidently wrong. Otherwise it is usual to post a *comment* only. This way the questioner can later choose easily which expert helped most. Thank you for listening...   (-:

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2788005
Ha Madshi, I just was writing a similar text :-)) You see this is an even better reason to create a standard text. I had to write this 3 or 4 times in the past two weeks and guess it is now time to make a default text...

saulite, welcome at E-E. Madshi has already writte what I wanted to express. One addition, though. You can use "withdraw answer" to "convert" the given answer to a normal comment. Thank you.

Ciao, Mike
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2788015
Hehe Mike, das *ist* mein Standard-Text. Ist in einer Textdatei unter meinen Favoriten gespeichert...   :-)   Aber vielleicht sollte ich das mit dem "withdraw" noch ergänzen.

Grüße... Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2788053
Okay, here's the text I derived. Perhaps one should create a FAQ list like in the newsgroups?

----------------------------
May I friendly lead your attention to an important issue? Here in the Delphi forum it is common practice to lock questions with an answer only if

you are the first expert in the thread, or if all other comments are evidently wrong. Otherwise it is usual to post a *comment* only. This way the

questioner can later easily choose which expert helped most. Additionally, using comments only is more polite because:

- otherwise it looks like you just want to jump the queue
- the question can easily be deleted if necessary
- it cannot happen that an answer is accidentally accepted
- you show that you acknowledge the work already done
- you give other experts the chance to post a better answer (although you have the final one, I know)

Please use the option "withdraw answer" to convert your answer to a normal comment.

Thank you for listening...   (-:

---------------------------------

Note, I have kept your special smiley form in the text to acknowledge the original author 8->

Ciao, Mike
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2788058
Ja, ja, ich weiss, die Zeilenumbrüche :-). Schon verbessert...
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2788404
Hi all.

Ich las diesen Text von Lischke schon wenn hier erstes Mal war;)
Jetzt sieht es um vieles besser aus.
I cut it for future usage, OK?
----
Igor.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2791790
Klar doch :-) I have worked out a more friendly text with help of Madshi. Here it is:

---------------------------------------
May I friendly lead your attention to an important issue? Here in the Delphi forum it is common practice to lock questions
with an answer only if you are the first expert in the thread, or if all other comments are evidently wrong. Otherwise it is
usual to post a *comment* only. This way the questioner can later easily choose which expert helped most. Additionally,
using comments only is more polite because:
 
- the question can easily be deleted if necessary
- it cannot happen that an answer is accidentally accepted
- you show that you acknowledge the work already done
- you give other experts a chance to post a better answer
 
Please, be so kind and use the option "withdraw answer" to convert your answer to a normal comment.
 
Thank you for listening...   (-:

---------------------------------------

Ciao, Mike
0
 

Author Comment

by:yaron_avraham
ID: 2800045
Thanks - Great answer !!
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2800075
Mmmh...
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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 91
Browsing a TTreeView in Delphi 5 49
Firemonkey Queue list 1 51
Posting a string with PostMessage 23 49
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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