i use this code in a thread (through Indy Onexecute event) . is there any problem ?
function TFrmMain.ShellExecute_AndWait(FileName, Params: string): bool;
var
exInfo: TShellExecuteInfo;
Ph: DWORD;
begin
FillChar(exInfo, SizeOf(exInfo), 0);
with exInfo do
begin
cbSize := SizeOf(exInfo);
fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
Wnd := GetActiveWindow();
exInfo.lpVerb := 'open';
exInfo.lpParameters := PChar(Params);
lpFile := PChar(FileName);
nShow := SW_NORMAL;
end;
if ShellExecuteEx(@exInfo) then
Ph := exInfo.hProcess
else
begin
Result := true;
exit;
end;
while WaitForSingleObject(exInfo.hProcess, 50) <> WAIT_OBJECT_0 do
begin
end;
CloseHandle(Ph);
Result := true;
end;
MSDN has this advice:
(In Delphi, you’d of course replace the first parameter with
niland useorfor the bitwise operation.)Raymond Chen recently wrote about the consequences of getting this wrong. The specific example was that the function might fail with the
Error_Access_Deniederror code.That’s the only potential multithreading issue I see in your code. Below are further things that occurred to me when I read your code, although they have nothing to do with multithreading (and not even much to do with Indy).
You have a peculiar way of waiting for the program to stop running. You repeatedly wait for 50 milliseconds at a time, but if the process isn’t finished yet, you do nothing but wait again. Describe your intention more accurately by specifying
Infinitefor the timeout.The function always returns
True. If there’s no useful return value, then you should just make it a procedure so there’s no return value at all. Don’t confuse the caller with useless information. If you’re going to keep it as a function, then use the Delphi native typeBooleaninstead of the Windows compatibility typeBoolfor the return type.I’m a little wary about the idea of a server executing user-interactive programs upon receipt of network messages.
Notice when MSDN says you might not get a process handle. There are cases when
ShellExecuteExcan service your request without creating a new process, so you’ll have nothing to wait on.The user might end up using the program awhile, and your server will be stuck waiting all that time. I wonder whether it really needs to wait at all. Is the client going to be waiting for a response from the server, too?