Improve company productivity with a Business Account.Sign Up

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

Function that informs me when EXE is finished

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
yaron_avraham
Asked:
yaron_avraham
  • 7
  • 4
  • 2
  • +8
1 Solution
 
yaron_avrahamAuthor Commented:
Adjusted points from 50 to 60
0
 
LischkeCommented:
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
 
ITugayCommented:
listening....
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Grayl1Commented:
Hi!

Try using Application.ProcessMessages; in stead of Sleep(3000);
0
 
NetoManCommented:
listen...
0
 
drnadeemCommented:
Listening
0
 
bryan7Commented:
listenning
0
 
GwenaCommented:
I'm listening too :-)
0
 
MadshiCommented:
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
 
LischkeCommented:
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
 
MadshiCommented:
:-)  That's nice...  (-:
0
 
johnstonedCommented:
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
 
sauliteCommented:
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
 
MadshiCommented:
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
 
LischkeCommented:
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
 
MadshiCommented:
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
 
LischkeCommented:
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
 
LischkeCommented:
Ja, ja, ich weiss, die Zeilenumbrüche :-). Schon verbessert...
0
 
ITugayCommented:
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
 
LischkeCommented:
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
 
yaron_avrahamAuthor Commented:
Thanks - Great answer !!
0
 
LischkeCommented:
Mmmh...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
  • +8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now