好友
阅读权限20
听众
最后登录1970-1-1
|
constructor TWaitRunThread.Create(const index:Integer; const CommandLine: string);
begin
inherited Create(true); //Suspend为真,立即被悬挂
FreeOnTerminate := True; //为真,运行完即删除
FCommandLine := CommandLine; Findex:=index;
end;
procedure TWaitRunThread.Execute; //执行线程
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
Handle: Boolean;
s:string;
i,u:Integer;
begin
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + FCommandLine), nil, nil, True, 0, nil, nil, SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then begin
try
u:=0;
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then begin
Buffer[BytesRead] := #0; u:=0;
s := StrPas(Buffer);
i:=pos('time=',s);
if i>0 then begin
Delete(s,1,i-1);
i:=pos('bitrate=',s);
FMsg:=Copy(s,1,i-1);
if(FMsg<>'')and Assigned(OnMsg) then begin
EnterCriticalSection(m_CS);
try
OnMsg(Self, Findex, FMsg);
except
end;
LeaveCriticalSection(m_CS);
end;
end;
end
else if (BytesRead = 0)or(WasOK=False)then begin
inc(u); Sleep(200);
end;
until (u>=3);
if mStop then begin
//TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, PI.dwProcessId),0);
FMsg:='停止';
end
else begin
//GetExitCodeProcess(ProcessInfo.hProcess, exitCode);
WaitForSingleObject(PI.hProcess, INFINITE);
FMsg:='完成'+FMsg;
end;
if Assigned(OnMsg) then OnMsg(Self, Findex, FMsg);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
//这是控制台,自己把里面没用的代码删除掉 |
|