L
liuchong
Unregistered / Unconfirmed
GUEST, unregistred user!
本意是想在线程中监测用CreateProcess启动的进程,在子进程退出时,自动结束自身进程。
但在主线程中使用“WaitForSingleObject”是阻塞的,这会影响发送键盘模拟
可是在子线程中“WaitForSingleObject”却反复执行,并不阻塞,代码如下:
TCheckProcess=class(TTHread)
private
I:Cardinal;
ProcessInfo:TProcessInformation;
procedure SetCaption;
protected
procedure Execute;override;
public
constructor Create(ProcessInfo:TProcessInformation);
end;
{ TCheckProcess }
constructor TCheckProcess.Create(ProcessInfo: TProcessInformation);
begin
inherited Create(False);
ProcessInfo:=ProcessInfo;
end;
procedure TCheckProcess.Execute;
begin
FreeOnTerminate:=True;
while Truedo
begin
//这里本应是阻塞的
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
Synchronize(SetCaption);
end;
end;
procedure TCheckProcess.SetCaption;
begin
Inc(I);
Form1.Caption:=IntToStr(I);
end;
主线程调用:
procedure TForm1.FormCreate(Sender: TObject);
var
StartupInfo:TStartUpInfo;
ProcessInfo:TProcessInformation;
MyThread:TCheckProcess;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),0);
with StartupInfodo
begin
cb:=SizeOf(StartupInfo);
dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;
lpTitle:=nil;
wShowWindow:=SW_Show;
end;
CreateProcess('a.exe',nil,nil,nil,true,DETACHED_PROCESS,nil,nil,StartupInfo,ProcessInfo);
MyThread:=TCheckProcess.Create(ProcessInfo);
//下面是模拟键盘代码
//...................
end;
但在主线程中使用“WaitForSingleObject”是阻塞的,这会影响发送键盘模拟
可是在子线程中“WaitForSingleObject”却反复执行,并不阻塞,代码如下:
TCheckProcess=class(TTHread)
private
I:Cardinal;
ProcessInfo:TProcessInformation;
procedure SetCaption;
protected
procedure Execute;override;
public
constructor Create(ProcessInfo:TProcessInformation);
end;
{ TCheckProcess }
constructor TCheckProcess.Create(ProcessInfo: TProcessInformation);
begin
inherited Create(False);
ProcessInfo:=ProcessInfo;
end;
procedure TCheckProcess.Execute;
begin
FreeOnTerminate:=True;
while Truedo
begin
//这里本应是阻塞的
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
Synchronize(SetCaption);
end;
end;
procedure TCheckProcess.SetCaption;
begin
Inc(I);
Form1.Caption:=IntToStr(I);
end;
主线程调用:
procedure TForm1.FormCreate(Sender: TObject);
var
StartupInfo:TStartUpInfo;
ProcessInfo:TProcessInformation;
MyThread:TCheckProcess;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),0);
with StartupInfodo
begin
cb:=SizeOf(StartupInfo);
dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;
lpTitle:=nil;
wShowWindow:=SW_Show;
end;
CreateProcess('a.exe',nil,nil,nil,true,DETACHED_PROCESS,nil,nil,StartupInfo,ProcessInfo);
MyThread:=TCheckProcess.Create(ProcessInfo);
//下面是模拟键盘代码
//...................
end;