Solved

Function that informs me when EXE is finished

Posted on 2000-05-03
22
201 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
  • 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now