?
Solved

Need advice about ExecAndWait, or the global var Application

Posted on 2007-03-20
8
Medium Priority
?
214 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Accepted Solution

by:
ziolko earned 1000 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: 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.

Question has a verified solution.

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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 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