iliescufm
asked on
See if an application is running
How can I see if an application which I have started from my program such as ftp.exe has been finished the execution?
Sorry forgot to check E-mail notify
Jacco is right. Here comes a full source example:
unit runThread_;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen}
public
{ Public-Deklarationen}
end;
var Form1 : TForm1 = nil;
implementation
{$R *.DFM}
const unitName = 'runThread_.';
type TRunThread = class(TThread)
private
processHandle : cardinal;
processReady : boolean;
waitingThread : cardinal;
procedure Execute; override;
end;
procedure TRunThread.Execute;
begin
WaitForSingleObject(proces sHandle,IN FINITE); // This call does not return, unless copy is stopped
processReady:=true; // Set "processReady" flag for main thread
PostThreadMessage(waitingT hread,WM_N ULL,0,0); // Wake up main thread
// If you call Application.HandleMessage (see below) in the
// main thread, the main thread is sleeping the most time in
// winAPI "waitMessage". So we send a "dummy" message in order
// to let the main thread return from Application.HandleMessage
end;
procedure TForm1.Button1Click(Sender : TObject);
var si : TStartupInfo;
pi : TProcessInformation;
dw1 : dword;
begin
enabled:=false;
caption:='start copy...';
ZeroMemory(@si,sizeOf(si)) ; si.cb:=sizeOf(si);
si.dwFlags:=STARTF_USESHOW WINDOW; si.wShowWindow:=SW_HIDE;
if CreateProcess(nil,'c:\comm and.com /c copy c:\autoexec.bat c:\test.bat >c:\output.txt',nil,nil,fa lse,0,nil, nil,si,pi) then begin
caption:='copy started...';
with TRunThread.Create(true) do // create the thread object, but do not start it now...
try
processHandle:=pi.hProcess ; // tell the thread what process it has to watch
processReady:=false; // flag for the loop (see below)
waitingThread:=GetCurrentT hreadID; // the current threadID for the wakeup message (see above)
caption:='wait for copy...';
Resume; // now all information is prepared; so let's start the thread
repeat
Application.HandleMessage; // message loop
until Application.Terminated or processReady; // continue with normal program when either the
// started process has stopped or our program is closed
caption:='copy stopped...';
finally Free end;
GetExitCodeProcess(pi.hPro cess,dw1);
CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
caption:='ready... (exitCode='+IntToStr(dw1)+ ')';
end else caption:='could not start copy...';
enabled:=true;
end;
end.
BTW, Jacco, you don't need to write a new comment to enable EMail notification. Just leave the edit field empty, don't check comment/answer, check the notify box and hit Submit. That works without adding a new (empty) comment. I've already tested that... :-)
unit runThread_;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen}
public
{ Public-Deklarationen}
end;
var Form1 : TForm1 = nil;
implementation
{$R *.DFM}
const unitName = 'runThread_.';
type TRunThread = class(TThread)
private
processHandle : cardinal;
processReady : boolean;
waitingThread : cardinal;
procedure Execute; override;
end;
procedure TRunThread.Execute;
begin
WaitForSingleObject(proces
processReady:=true; // Set "processReady" flag for main thread
PostThreadMessage(waitingT
// If you call Application.HandleMessage (see below) in the
// main thread, the main thread is sleeping the most time in
// winAPI "waitMessage". So we send a "dummy" message in order
// to let the main thread return from Application.HandleMessage
end;
procedure TForm1.Button1Click(Sender
var si : TStartupInfo;
pi : TProcessInformation;
dw1 : dword;
begin
enabled:=false;
caption:='start copy...';
ZeroMemory(@si,sizeOf(si))
si.dwFlags:=STARTF_USESHOW
if CreateProcess(nil,'c:\comm
caption:='copy started...';
with TRunThread.Create(true) do // create the thread object, but do not start it now...
try
processHandle:=pi.hProcess
processReady:=false; // flag for the loop (see below)
waitingThread:=GetCurrentT
caption:='wait for copy...';
Resume; // now all information is prepared; so let's start the thread
repeat
Application.HandleMessage;
until Application.Terminated or processReady; // continue with normal program when either the
// started process has stopped or our program is closed
caption:='copy stopped...';
finally Free end;
GetExitCodeProcess(pi.hPro
CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
caption:='ready... (exitCode='+IntToStr(dw1)+
end else caption:='could not start copy...';
enabled:=true;
end;
end.
BTW, Jacco, you don't need to write a new comment to enable EMail notification. Just leave the edit field empty, don't check comment/answer, check the notify box and hit Submit. That works without adding a new (empty) comment. I've already tested that... :-)
Thanx Mashdi!
Madshi! Not Mashdi... :-)
It's really easy, just remember, I'm mad... "mad-shi"...
shi is really mad, hehe.... :))
btw, Madshi, it does not work with just checking the box and submitting with the edit box being empty.... that's not gonna work, because as soon as the answer is accepted you won't be able to view the question, unless you've added a REAL comment to it... Do you understand what I'm trying to say?? I've tested it... that way, if you're learning from other questions that people answer, you'll not get the final solution to a problem especially if you';re away form the computer and then you can't go back and view the whole question etc...
-vik-
btw, Madshi, it does not work with just checking the box and submitting with the edit box being empty.... that's not gonna work, because as soon as the answer is accepted you won't be able to view the question, unless you've added a REAL comment to it... Do you understand what I'm trying to say?? I've tested it... that way, if you're learning from other questions that people answer, you'll not get the final solution to a problem especially if you';re away form the computer and then you can't go back and view the whole question etc...
-vik-
Hi Vik,
though, I'm a he, not a shi... :-)
not in this case. Jacco already had written a comment to this question. So in this case it would have worked perfectly with only checking the box.
But generally you're right. Without writing a "real" comment to a question, you can't read the comments/answer, once the answer is accepted, without paying for it.
Regards, Madshi.
though, I'm a he, not a shi... :-)
not in this case. Jacco already had written a comment to this question. So in this case it would have worked perfectly with only checking the box.
But generally you're right. Without writing a "real" comment to a question, you can't read the comments/answer, once the answer is accepted, without paying for it.
Regards, Madshi.
>>though, I'm a he, not a shi... :-)
are you sure about that??
hehe, just kiddin' ;-)))
how is it goin around here?
are you sure about that??
hehe, just kiddin' ;-)))
how is it goin around here?
Well, it's goin ok. Not many shis here for a he like me... :-(
hehe... :))
Question answered?
Regards Jacco
I'll remember Madshi
Regards Jacco
I'll remember Madshi
well Madshi, I can transfer you in a 'she' if you want. I've got a spell somewhere...
AAAAAAAAAH - don't - *PLEASE*!!!
There's a fantastic girl in my church waiting to be dated someday (when she's old enough). It would be a quite bad spell for me... :-)
There's a fantastic girl in my church waiting to be dated someday (when she's old enough). It would be a quite bad spell for me... :-)
hoho, how old is she now? 5? hehe ;))
No - 17, but not grownup enough... :-)
you call me not grown up enough?? I mean 'cuz I'm 17 too ;D
Hehe... :-)
I didn't mean that all 17 year old humans are not grownup enough. I meant THIS one specific girl is with her 17 years not grownup enough for me... (-:
I didn't mean that all 17 year old humans are not grownup enough. I meant THIS one specific girl is with her 17 years not grownup enough for me... (-:
hehe okay ;D
ASKER
Thanks to Jacco, and al lot of thanks to Madshi.
ASKER
Thanks to Jacco, and a lot of thanks to Madshi.
Hi lilescufm, to whom do you want to give the points? :-)
ASKER
I want to give the points to Madshi, how can I do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
P.S: Sorry to Jacco...
p := CreateProcess(....)
then you do:
while WaitForSingleObject(p, 100) = WAIT_TIMEOUT do ;
Good luck..
If you need to know how to fil the params of CreateProcess respond
Regards Jacco