Solved

Need advice about ExecAndWait, or the global var Application

Posted on 2007-03-20
8
205 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
Comment Utility
you can always pass Application as param to this function:)

ziolko.
0
 
LVL 6

Author Comment

by:Hardi
Comment Utility
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
Comment Utility
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
 
LVL 21

Accepted Solution

by:
ziolko earned 250 total points
Comment Utility
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
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!

 
LVL 6

Author Comment

by:Hardi
Comment Utility
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
Comment Utility
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
Comment Utility
Yup me neither, okay I'll use your code. Thanks very much! :-D
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
glad i could help:)

ziolko.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

12 Experts available now in Live!

Get 1:1 Help Now