• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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?
0
iliescufm
Asked:
iliescufm
  • 11
  • 6
  • 4
  • +2
1 Solution
 
JaccoCommented:
You can use CreateProcess and store the resulting process parameter.

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
0
 
JaccoCommented:
Sorry forgot to check E-mail notify
0
 
MadshiCommented:
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(processHandle,INFINITE);   // This call does not return, unless copy is stopped
  processReady:=true;                            // Set "processReady" flag for main thread
  PostThreadMessage(waitingThread,WM_NULL,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_USESHOWWINDOW; si.wShowWindow:=SW_HIDE;
  if CreateProcess(nil,'c:\command.com /c copy c:\autoexec.bat c:\test.bat >c:\output.txt',nil,nil,false,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:=GetCurrentThreadID;  // 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.hProcess,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...   :-)
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
JaccoCommented:
Thanx Mashdi!
0
 
MadshiCommented:
Madshi! Not Mashdi...   :-)
0
 
MadshiCommented:
It's really easy, just remember, I'm mad... "mad-shi"...
0
 
viktornetCommented:
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-
0
 
MadshiCommented:
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.
0
 
viktornetCommented:
>>though, I'm a he, not a shi...   :-)
are you sure about that??



hehe, just kiddin' ;-)))

how is it goin around here?
0
 
MadshiCommented:
Well, it's goin ok. Not many shis here for a he like me...   :-(
0
 
viktornetCommented:
hehe... :))
0
 
JaccoCommented:
Question answered?

Regards Jacco

I'll remember Madshi
0
 
ZifNabCommented:
well Madshi, I can transfer you in a 'she' if you want. I've got a spell somewhere...
0
 
MadshiCommented:
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...   :-)
0
 
viktornetCommented:
hoho, how old is she now? 5? hehe ;))
0
 
MadshiCommented:
No - 17, but not grownup enough...   :-)
0
 
viktornetCommented:
you call me not grown up enough?? I mean 'cuz I'm 17 too ;D
0
 
MadshiCommented:
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...  (-:
0
 
viktornetCommented:
hehe okay ;D
0
 
iliescufmAuthor Commented:
Thanks to Jacco, and al lot of thanks to Madshi.
0
 
iliescufmAuthor Commented:
Thanks to Jacco, and a lot of thanks to Madshi.
0
 
MadshiCommented:
Hi lilescufm, to whom do you want to give the points?   :-)
0
 
iliescufmAuthor Commented:
I want to give the points to Madshi, how can I do that?
0
 
MadshiCommented:
I must first answer the question. Now you can accept my answer. Thanx for the points...   :-)
0
 
MadshiCommented:
P.S: Sorry to Jacco...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now