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
Solved

Need advice about ExecAndWait, or the global var Application

Posted on 2007-03-20
8
209 Views
Last Modified: 2010-04-05
I have been using this function to execute an external application and wait until it finishes before executing the next line
http://delphi.about.com/od/windowsshellapi/a/executeprogram.htm (scroll down)
It's perfect.

But now because I use it in a number of applications, I am thinking of putting this function into a new unit, so I can just include this unit in each application rather than copy-pasting the whole code.
However it involves Application global variable, should I include Forms in my unit? Or is there a more suitable function to be put in a separate unit?

(I need the function to wait until the external app finishes before executing the next line, and can take arguments for running the external app)

Thank you in advance
0
Comment
Question by:Hardi
  • 5
  • 3
8 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 18755023
you can always pass Application as param to this function:)

ziolko.
0
 
LVL 6

Author Comment

by:Hardi
ID: 18755282
I know... so I have to include Forms in my unit?
Is there any better solutions? Like changing the function so it doesn't use Application?

Because in a console application, including Forms can increase the filesize a lot... right now I'm only using it in windows applications though...
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18755338
yes, you need to add Forms to new unit.

however if you want to avoid using Application (which requires forms) it's possible with CreateProcess() and one of waiting functions (like WaitForMultipleObjects)

ziolko.
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.

 
LVL 21

Accepted Solution

by:
ziolko earned 250 total points
ID: 18755375
something like this:


procedure TForm1.Button1Click(Sender: TObject);
var start_up: STARTUPINFO;
    proc_inf: PROCESS_INFORMATION;
begin
  FillChar(start_up, SizeOf(start_up), 0);
  start_up.cb := SizeOf(start_up);
  start_up.dwFlags := STARTF_USESHOWWINDOW;
  start_up.wShowWindow := SW_SHOWNORMAL;
  FillChar(proc_inf, SizeOf(proc_inf), 0);
  if CreateProcess('c:\windows\notepad.exe', nil, nil, nil, True, 0, nil, nil, start_up, proc_inf) then begin
    WaitForSingleObject(proc_inf.hProcess, INFINITE);
    ShowMessage('done');
  end else
    ShowMessage(SysErrorMessage(GetLastError));
end;

ziolko.
0
 
LVL 6

Author Comment

by:Hardi
ID: 18762516
It works, thank you ziolko! It's robust isn't it?
So in your opinion, which one is better to be put into a new unit?
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18762562
well it depends, in console app CreateProcess and WaitForSingleObject for sure is better
also when you need you're app to freeze until created process finishes its execution.

but in case you want you're app to be working and only be notified when launched process exited
you can use method you found on delphi.about, but personally i don't like anything that has loop
with Application.ProcessMessages

ziolko.
0
 
LVL 6

Author Comment

by:Hardi
ID: 18762569
Yup me neither, okay I'll use your code. Thanks very much! :-D
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18762581
glad i could help:)

ziolko.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 164
Delphi: how to send PJL commands to printer 3 108
How to build JSON File in Delphi 6 3 45
Installshield for Embarcadero EX 10.1 Berlin 4 60
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

840 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