Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to run another program and wait until it's finished. Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
run and wait a exe to finish. 22-Jan-04
Category
Win API
Language
Delphi All Versions
Views
661
User Rating
8.666666
# Votes
3
Replies
0
Publisher:
Darley, F. Joe
Reference URL:
			1   
2   function TForm1.RunProgramWait ( ProgramName, CommandLine : string; 
3   dwCreationFlags: dWord = 0; HowToShowWindow :dWord=SW_SHOWNORMAL) : integer;
4   var
5     StartupInfo : TStartupInfo;
6     ProcessInfo : TProcessInformation;
7     Code        : Cardinal;
8   begin
9     FillChar(StartupInfo, sizeof(TStartupInfo),0)
10    with StartupInfo do   //recreates record for start
11    begin
12    cb := SizeOf( StartupInfo);
13    dwFlags := STARTF_USESHOWWINDOW;
14    wShowWindow := HowToShowWindow;
15    end;
16  
17    Code := 0;
18  
19    Application.Hint := 'Starting ' + ProgramName + '...';
20    if CreateProcess(nil, PChar(ProgramName + ' ' + CommandLine),
21        nil, nil,  False, dwCreationFlags, nil, PChar(ExtractFilePath(ProgramName)),
22        StartupInfo, ProcessInfo) then      //starts the application
23      begin
24        caption := ProgramName + ' -Active';
25        //loop to check if application is terminated or not
26        while GetExitCodeProcess(ProcessInfo.hProcess, Code) and (Code = 
27  STILL_ACTIVE) do
28          begin
29            Application.ProcessMessages;
30            Sleep(1000); //to reduce CPU load the higher the better.
31          end;
32        Result := Code;
33      end
34    else
35      Result := 99;
36  
37    caption := ProgramName + ' -NOT Active';
38  end;
39  
40  procedure TForm1.Button1Click(Sender: TObject);
41  var
42  i: integer;
43  begin
44  //you can use SW_SHOWNORMAL or SW_HIDE
45  i:=RunProgramWait('c:\WINNT\system32\cmd.exe', '', 0, SW_SHOWNORMAL);
46  
47  if i=0 then
48   showmessage('Normal Termination')
49  else
50   showmessage('Abnormal Termination')
51  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC