Solved

Need advice about ExecAndWait, or the global var Application

Posted on 2007-03-20
8
208 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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