Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WaitForSingleObject(...,TimeOut) = freeze my application

1,397 views
Skip to first unread message

Eric Mathiot

unread,
Jul 26, 1999, 3:00:00 AM7/26/99
to
In my component, I use WaitForSingleObject(...,TimeOut). But my application
is freezed.

How can I do for avoid it ? Can you send me an example?

Sorry for my poor English.
Thanks.

Paul Shay

unread,
Jul 26, 1999, 3:00:00 AM7/26/99
to
Create a thread and use the waitforsingleobject within the thread.

Paul


Eric Mathiot wrote in message <7nia2m$3b...@forums.borland.com>...

krinn

unread,
Jul 27, 1999, 3:00:00 AM7/27/99
to
Eric Mathiot wrote:

> In my component, I use WaitForSingleObject(...,TimeOut). But my application
> is freezed.
>
> How can I do for avoid it ? Can you send me an example?
>
> Sorry for my poor English.
> Thanks.

This exec an app and wait until its end.... (see the Wait sample)
Krinn

function CreateProcessAndWait(const AppPath, AppParams: String; Visibility:
word): DWord;
var
SI: TStartupInfo;
PI: TProcessInformation;
Proc: THandle;
begin
AppParams:=AppPath+' '+AppParams;
{This force paramstr(0) to be program path, like Delphi & BP does}
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
SI.wShowWindow := Visibility;
if not CreateProcess(PChar(AppPath), PChar(AppParams), Nil, Nil, False,
Normal_Priority_Class, Nil, Nil, SI, PI) then
raise Exception.CreateFmt('Failed to execute program. Error Code %d',
[GetLastError]);
Proc := PI.hProcess;
CloseHandle(PI.hThread);
if WaitForSingleObject(Proc, Infinite) <> Wait_Failed then
GetExitCodeProcess(Proc, Result);
CloseHandle(Proc);
end;

Praful Kapadia

unread,
Jul 27, 1999, 3:00:00 AM7/27/99
to
Eric

WaitForSingleObject must be used in a thread (not the primary thread). If
you want to wait for one or more objects in the primary thread, use
MsgWaitForMultipleObjects. The D4 example below starts a program and,
optionally, waits for it to finish before returning. When waiting, it uses
MsgWaitForMultipleObjects. To prevent the project from freezing, the
following lines are used:

while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;

// Run program and/or open document.
//aAction could be eg 'open, 'explore' (for folders), 'print'
//if aWait is true, wait for program to end before returning
procedure pkShellExecute( const aCmd: string;
const aCurrentDirectory: TFilename = '';
const aWait: Boolean = False;
const aWindowState: integer = SW_SHOW;
const aAction: string = 'open');
var
seInfo: TShellExecuteInfo;
isProcessRunning: Boolean;
WaitState: DWORD;
Msg: TMsg;
begin
with seInfo do begin
cbSize := SizeOf(TShellExecuteInfo);
fMask := SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS or
SEE_MASK_FLAG_DDEWAIT;
lpVerb := PChar(aAction);
lpFile := PChar(aCmd);
lpParameters := nil;
if aCurrentDirectory = '' then
lpDirectory := nil
else
lpDirectory := PChar(aCurrentDirectory);
nShow := aWindowState;
end; //with

Win32Check(ShellExecuteEx(@seInfo));

if seInfo.hProcess <> 0 then begin
if aWait then begin
isProcessRunning := True;
while isProcessRunning do begin
WaitState := MsgWaitForMultipleObjects(
1,
seInfo.hProcess,
False,
INFINITE,
QS_ALLINPUT);

case WaitState of

//process finished
WAIT_OBJECT_0: begin
isProcessRunning := false;
end;

//Message sent
WAIT_OBJECT_0 + 1: begin
// read all of the messages in this next loop
// removing each message as we read it
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end; //case
end; //while
end; //wait
CloseHandle(seInfo.hProcess);
end; //hProcess
end; //proc

Regards
Praful
(Remove spam-spoiler "ABC" from email address)

Eric Mathiot wrote in message <7nia2m$3b...@forums.borland.com>...

Daniel J. Wojcik

unread,
Aug 1, 1999, 3:00:00 AM8/1/99
to
On Tue, 27 Jul 1999 00:26:51 +0100, "Praful Kapadia"
<ABCP...@btinternet.com> wrote:

>// Run program and/or open document.
>//aAction could be eg 'open, 'explore' (for folders), 'print'
>//if aWait is true, wait for program to end before returning
>procedure pkShellExecute( const aCmd: string;
> const aCurrentDirectory: TFilename = '';
> const aWait: Boolean = False;
> const aWindowState: integer = SW_SHOW;
> const aAction: string = 'open');
>var
> seInfo: TShellExecuteInfo;
> isProcessRunning: Boolean;
> WaitState: DWORD;
> Msg: TMsg;
>begin

etc...


I just tried the above procedure and it did absolutely nothing. Just
trying to print a doc with Word.

If I step thru, the Win32Check says there's not enough memory to do
it. Nonsense. The machine has plenty of memory, and most of it was
available.

Any ideas?

Daniel J. Wojcik
****************
http://www.genjerdan.com

programming, and other things best done in private

Praful Kapadia

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
>If I step thru, the Win32Check says there's not enough memory to do
>it. Nonsense. The machine has plenty of memory, and most of it was
>available.
>
>Any ideas?
>

I don't why the Windows ShellExecuteEx API is returning an error. It's
obviously a consequential error if you have enough memory (perhaps related
to the printer).

I tried the following successfully:

pkShellExecute('c:\temp\a.txt', '', True, 5, 'open');
ShowMessage('Returned');

This opens Notepad and then display "Returned" when you quit Notepad.

Regards
Praful
(Remove spam-spoiler "ABC" from email address)

>[...]
>Daniel J. Wojcik
>****************


0 new messages