Solved

Need advice about ExecAndWait, or the global var Application

Posted on 2007-03-20
8
210 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working this component WebCopy with proxy, help 3 103
Delphi - replicating a form 8 93
When i run adoquery my application freezes 26 192
Delphi: making a BW image transparent 10 96
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

730 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