使用线程,列举进程,杀QQ
1,定义一个线程类与定义一个普通类没什么区别,只是把主类设为TThead
TThread类定义在class单元中
---------------------------------------
2,一个自定义线程类必须定义Execute过程这个过程为Override,需要线程去做的事情
全部写在这个方法里
---------------------------------------------
例如:
type TMyThread = class(TThread)
private
protected
procedure Execute;override;
public
end;
-------------------------------------
{ TMyThread }
var
i : Integer;
procedure TMyThread.Execute;
begin
inherited;
FreeOnTerminate := True; //如果线程终止就释放线程所占用的资源
for i := 0 to 10000 do begin
Form1.Caption := IntToStr(i);
end;
end;
-------------例举进程并杀QQ(Uses TLHelp32)------------
var
ProcessName : string;
ProcessID : DWORD;
SnapShotHandle : THandle;
ProcessEnter : TProcessEntry32;
Loop : Boolean;
begin
SnapShotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
ProcessEnter.dwSize := Sizeof(ProcessEnter);
Loop := Process32First(SnapShotHandle,ProcessEnter);
while Loop do begin
ProcessName := ProcessEnter.szExeFile;
ProcessID := ProcessEnter.th32ProcessID;
Memo1.Lines.Add(ProcessName + ' ' + IntToStr(ProcessID));
//-----杀QQ-----------------
if ProcessName = 'QQ.exe' then begin
TerminateProcess(OpenProcess(PROCESS_TERMINATE,True,ProcessID),0);
Memo1.Lines.Add('----------------------');
Memo1.Lines.Add('Exit QQ');
Memo1.Lines.Add('----------------------');
end;
Loop := Process32Next(SnapShotHandle,ProcessEnter)
end;
end;
TThread类定义在class单元中
---------------------------------------
2,一个自定义线程类必须定义Execute过程这个过程为Override,需要线程去做的事情
全部写在这个方法里
---------------------------------------------
例如:
type TMyThread = class(TThread)
private
protected
procedure Execute;override;
public
end;
-------------------------------------
{ TMyThread }
var
i : Integer;
procedure TMyThread.Execute;
begin
inherited;
FreeOnTerminate := True; //如果线程终止就释放线程所占用的资源
for i := 0 to 10000 do begin
Form1.Caption := IntToStr(i);
end;
end;
-------------例举进程并杀QQ(Uses TLHelp32)------------
var
ProcessName : string;
ProcessID : DWORD;
SnapShotHandle : THandle;
ProcessEnter : TProcessEntry32;
Loop : Boolean;
begin
SnapShotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
ProcessEnter.dwSize := Sizeof(ProcessEnter);
Loop := Process32First(SnapShotHandle,ProcessEnter);
while Loop do begin
ProcessName := ProcessEnter.szExeFile;
ProcessID := ProcessEnter.th32ProcessID;
Memo1.Lines.Add(ProcessName + ' ' + IntToStr(ProcessID));
//-----杀QQ-----------------
if ProcessName = 'QQ.exe' then begin
TerminateProcess(OpenProcess(PROCESS_TERMINATE,True,ProcessID),0);
Memo1.Lines.Add('----------------------');
Memo1.Lines.Add('Exit QQ');
Memo1.Lines.Add('----------------------');
end;
Loop := Process32Next(SnapShotHandle,ProcessEnter)
end;
end;