Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function that informs me when EXE is finished

Posted on 2000-05-03
22
Medium Priority
?
216 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 240 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

670 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