你的意思是说你想要结束某一段时间内运行的程序吗?如果是这样的话我觉得可以这样做:、
先得到所有的当前运行的进程,过一段时间后取得新的进程序列,比较之后删除新增的。
下面是列出说与进程和结束进程的例子,其中只有一ListBox和Button。希望对你有所帮助。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons,TLHelp32,shellapi;
type
TProcessInfo=Record
ExeFile:string;
ProcessID
word;
end;
ProcessInfo=^TProcessInfo;//////////
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure ProcessList(var pList: TList);
{ Private declarations }
public
Current:TList;////////////
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ProcessList(Var pList:TList);
var p
rocessInfo;
ok:Bool;
ProcessListHandle:THandle;
ProcessStruct:TProcessEntry32;
begin
pList:=TList.Create;
pList.Clear;
ProcessListHandle:=CreateToolHelp32SnapShot(TH32cs_SnapProcess,0);
ProcessStruct.dwSize:=SizeOf(ProcessStruct);
ok:=Process32First(ProcessListHandle,ProcessStruct);
while integer(ok)<>0 do
begin
New(p);
p.ExeFile:=ProcessStruct.szExeFile;
p.ProcessID:=ProcessStruct.th32ProcessID;
pList.Add(p);
ok:=Process32Next(ProcessListHandle,ProcessStruct);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
p
rocessInfo;
begin
current:=TList.Create;
current.Clear;
ListBox1.Clear;
ProcessList(current);
for i:=0 to current.Count-1 do
begin
new(p);
p:=Current.Items
;
ListBox1.Items.Add(p.ExeFile);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var h:Thandle;
aword;
processInfo;
begin
if ListBox1.ItemIndex>=0 then
begin
p:=Current.Items[ListBox1.ItemIndex];
h:=OpenProcess(Process_All_Access,true,p.ProcessID);
GetExitCodeProcess(h,a);
if integer(TerminateProcess(h,a))<>0 then //success!
begin
/////
end
else ShowMessage('关闭进程'+#13
+ListBox1.Items[ListBox1.ItemIndex]+#13
+'失败!');
end;
end;
end.