I am using this procedure to execute a Comandline. I have found this piece of code online and it works fine, except a few details. I have read in some forums not to use ProcessMessages
and put it into a thread.
When I remove the Application.ProcessMessages
line, then it stops working.
Then if I keep it, while it's executing I get "Not responding"
. Could you help me in using a thread in this case?
procedure ExecAndWait(const CommandLine: string);
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(TStartupInfo);
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
//UniqueString(CommandLine);
if CreateProcess(nil, PChar(CommandLine), nil, nil, False,
0, nil, nil, StartupInfo, ProcessInfo) then
begin
while WaitForSingleObject(ProcessInfo.hProcess, 10) > 0 do
Application.ProcessMessages;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end
else
RaiseLastOSError;
end;
end.
procedure BuildThread;
var
myThread: TThread;
begin
// Create an anonymous thread that calls a method and passes in
// the fetchURL to that method.
myThread := TThread.CreateAnonymousThread(
procedure
begin
ExecAndWait();
end);
end;
I added this:
procedure RunThread(const CommandLine: string);
var
myThread: TThread;
begin
myThread := TThread.CreateAnonymousThread(
procedure
begin
ExecAndWait(CommandLine);
end). Start;
end;