Please, what am I doing wrong?
Thanks a lot! Vojta
ryvo (at) centrum . cz
program MyProgram;
uses Windows, Messages, Classes;
type
TExeRunner = class(TThread)
procedure Execute; override;
end;
TMainObject = class
ExeRunner : TExeRunner;
constructor Create;
destructor Destroy; override;
procedure ExeRunnerTerminate(Sender: TObject); // Thread OnTerminate
event handler
end;
constructor TMainObject.Create;
begin
ExeRunner:= TExeRunner.Create(True);
ExeRunner.FreeOnTerminate:= True;
ExeRunner.OnTerminate:= ExeRunnerTerminate;
ExeRunner.Resume;
end;
destructor TMainObject.Destroy;
begin
if Assigned(ExeRunner) then
begin
ExeRunner.OnTerminate:= nil;
ExeRunner.Terminate;
end;
inherited Destroy;
end;
procedure TMainObject.ExeRunnerTerminate(Sender: TObject);
begin
PostQuitMessage(0);
end;
procedure TExeRunner.Execute;
begin
// Start exe and wait untilk it terminates (CreateProcess,
WaitForSingleObject)
Terminate;
end;
var
Msg: TMsg;
wClass: TWndClass;
hAppHandle: HWND;
MainObject: TMainObject;
function WindowProc(hWnd, Msg, wParam, lParam: Integer): Integer; stdcall;
begin
Result := 0;
case Msg of
WM_CREATE:
begin
Coinitialize(nil);
MainObject:= TMainObject.Create;
end;
WM_DESTROY:
begin
MainObject.Free;
PostQuitMessage(0);
end;
end;
Result := DefWindowProc(hWnd,Msg,wParam,lParam);
end;
function CreateWindow: HWND;
begin
wClass.lpszClassName:= 'CN';
wClass.lpfnWndProc:= @WindowProc;
wClass.hInstance:= hInstance;
wClass.hbrBackground:= 1;
RegisterClass(wClass);
Result:= Windows.CreateWindow(wClass.lpszClassName, nil, 0, 0, 0, 0, 0,
0, 0, hInstance, nil);
end;
begin
hAppHandle:= CreateWindow;
if hAppHandle = 0 then
begin
UnRegisterClass(wClass.lpszClassName, hInstance);
Abort;
end;
while GetMessage(Msg,0,0,0) do
begin
DispatchMessage(Msg);
end;
end.
Check Help: Terminate will not terminate the thread. It merely sets the
terminated property. In this code it means that execute just finishes, which
should lead to a call to the OnTerminate method.
Tom
Why do you use an additional thread?
DoDi
when I launched (CreateProcess) EXE directly from the application and waited
for it (WaitForSingleObject) it looked like suspended until
WaitForSingleObject timed out, then the EXE "resumed". This very strange
behaviour appears when my application is launched by another
16bit application but only on some computers (Win XP Pro SP2). So I thought
it will work when I launch it from separate thread but it does not.
The EXE calls MACHNM1.EXE. If the timeout is too long EXE reports "Cannot
comunicate with MACHNM1.EXE" and terminates.
Vojta
"Hans-Peter Diettrich" <DrDiet...@aol.com> píse v diskusním príspevku
news:4r2jh7F...@individual.net...
>Hello DoDi,
>
>when I launched (CreateProcess) EXE directly from the application and waited
>for it (WaitForSingleObject) it looked like suspended until
>WaitForSingleObject timed out, then the EXE "resumed". This very strange
>behaviour appears when my application is launched by another
>16bit application but only on some computers (Win XP Pro SP2). So I thought
>it will work when I launch it from separate thread but it does not.
>The EXE calls MACHNM1.EXE. If the timeout is too long EXE reports "Cannot
>comunicate with MACHNM1.EXE" and terminates.
If I understand you correctly,
1) You have an App that launches another EXE
2) You are waiting for something using WaitForSingleObject
3) You have a timeout that is not INFINITE
I would expect your launching App to freeze on any machine, unless you
had a shorter timeout and processed the Windows message queue using
Application.ProcessMessages or Application.HandleMessage in a loop
I experimented and created clean application - one form with one button.
Button press calls CreateProcess and launches the EXE. Nothing more. EXE
starts but looks suspended until I close my application. Then it starts
running and finishes it's task.
This problem appears on some systems only and I cannot find any dependency
(the same Win XP Pro SP2 OS ...). Very very strange.
Vojta
"J French" <ere...@nowhere.uk> píąe v diskusním příspěvku
news:454c53b0....@news.btclick.com...
>Yes, my app launches another EXE (console app) using CreateProcess and then
>waits for exe for let's say 10 second using WaitForSingleObject and then
>exits. If EXE does not finish within 10 secs I assume it has failed
>(timeout) and display error mesage. That is ok that my app freezes if I do
>not create and wait for EXE in separate thread. What I do no understand is
>why the launched EXE gets suspended when my application is still running and
>resumes as soon as my app terminates.
>I experimented and created clean application - one form with one button.
>Button press calls CreateProcess and launches the EXE. Nothing more. EXE
>starts but looks suspended until I close my application. Then it starts
>running and finishes it's task.
Just a hunch
- in CreateProcess have a look at the CREATE_SUSPENDED flag in the
dwCreationFlags parameter
CREATE_SUSPENDED
The primary thread of the new process is created in a suspended state,
and does not run until the ResumeThread function is called.
> Yes, my app launches another EXE (console app) using CreateProcess and then
> waits for exe for let's say 10 second using WaitForSingleObject and then
> exits. If EXE does not finish within 10 secs I assume it has failed
> (timeout) and display error mesage. That is ok that my app freezes if I do
> not create and wait for EXE in separate thread.
That's why I didn't understand the need for an additional thread.
> What I do no understand is
> why the launched EXE gets suspended when my application is still running and
> resumes as soon as my app terminates.
This behaviour might be enforced by some (missing?) settings in
CreateProcess. I don't know about details, but I could imagine that the
exe, when started as a sub-process, will be suspended together with your
app, when waiting for a timed event.
You have many chances for bad parameters in the CreateProcess call.
Please check all arguments, and don't rely on meaningful defaults on
every Windows version :-(
DoDi
"Hans-Peter Diettrich" <DrDiet...@aol.com> píse v diskusním príspevku
news:4r3h68F...@individual.net...