怎么中止进程,请帮我看一下,谢谢 ( 积分: 100 )

  • 主题发起人 主题发起人 zhoupj
  • 开始时间 开始时间
Z

zhoupj

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
我的窗体上有三个edit控件,两个button按钮,按button1起动1个线程(放在UNIT2中),线程中用消息传出变量值,在窗体中的EDIT显示,其中edit1显示线程号,edit2显示值1,edit3显示值2,button2的功能是如线程没有结束时可以中断线程,已结束则提示已结束,中断或结束线程后BUTTON1按下又可重新开始
我已做了BUOONT1的程序,button2的怎么做呢,不懂了,请高手帮忙啊,程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Timer1: TTimer;
Edit3: TEdit;
Button2: TButton;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure CreateParams(Var Params:TCreateParams);
procedure Wndpro(Var Message:TMessage);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.CreateParams(var Params: TCreateParams);
var
str:string;
begin
inherited CreateParams(Params);
str:=Form1.Caption;
Params.Caption:=Pchar(str);
end;

procedure TForm1.Wndpro(var Message: TMessage);
var
processhnd:Thandle;
numrc:dword;
begin
processhnd:=Openprocess(PROCESS_VM_READ,False,Message.WParam);
Readprocessmemory(processhnd,ptr(Message.LParam),@(unit2.ssaa),Sizeof(unit2.ssaa),numrc);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
edit1.text:=inttostr(unit2.ssaa.single);
edit2.text:=inttostr(unit2.ssaa.a);
edit3.text:=inttostr(unit2.ssaa.b);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
unit2.test.create;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//怎么写呢,这里不会了
end;

end.

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ExtCtrls;
type
test = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
Constructor Create;
end;

const
my_definemsg=WM_user+1001;
type
Ta=Packed record
single:Longword;
a:integer;
b:integer;
end;

var
ssaa:Ta;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ test }
constructor test.Create;
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
end;

procedure test.Execute;
var
i,j:integer;
wnd:Hwnd;
begin
{ Place thread code here }
for i:=0 to 10000000 do
begin
ssaa.a:=i;
for j:=0 to 1000000do
begin
ssaa.b:=j;
//生成监控信息
wnd:=FindWindow(nil,'Form1');

if wnd<>0 then
begin
ssaa.single:= GetCurrentProcessID;
SendMessage(wnd ,my_definemsg,GetCurrentProcessID,Lparam(@ssaa));
end;
//生成监控信息 结束
end;
end;
end;

end.
 
代码:
我的窗体上有三个edit控件,两个button按钮,按button1起动1个线程(放在UNIT2中),线程中用消息传出变量值,在窗体中的EDIT显示,其中edit1显示线程号,edit2显示值1,edit3显示值2,button2的功能是如线程没有结束时可以中断线程,已结束则提示已结束,中断或结束线程后BUTTON1按下又可重新开始
我已做了BUOONT1的程序,button2的怎么做呢,不懂了,请高手帮忙啊,程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Timer1: TTimer;
Edit3: TEdit;
Button2: TButton;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure CreateParams(Var Params:TCreateParams);
procedure Wndpro(Var Message:TMessage);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.CreateParams(var Params: TCreateParams);
var
str:string;
begin
inherited CreateParams(Params);
str:=Form1.Caption;
Params.Caption:=Pchar(str);
end;

procedure TForm1.Wndpro(var Message: TMessage);
var
processhnd:Thandle;
numrc:dword;
begin
processhnd:=Openprocess(PROCESS_VM_READ,False,Message.WParam);
Readprocessmemory(processhnd,ptr(Message.LParam),@(unit2.ssaa),Sizeof(unit2.ssaa),numrc);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
edit1.text:=inttostr(unit2.ssaa.single);
edit2.text:=inttostr(unit2.ssaa.a);
edit3.text:=inttostr(unit2.ssaa.b);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
unit2.test.create;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//怎么写呢,这里不会了
end;

end.

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ExtCtrls;
type
test = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
Constructor Create;
end;

const
my_definemsg=WM_user+1001;
type
Ta=Packed record
single:Longword;
a:integer;
b:integer;
end;

var
ssaa:Ta;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ test }
constructor test.Create;
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
end;

procedure test.Execute;
var
i,j:integer;
wnd:Hwnd;
begin
{ Place thread code here }
for i:=0 to 10000000 do
begin
ssaa.a:=i;
for j:=0 to 1000000do
begin
ssaa.b:=j;
//生成监控信息
wnd:=FindWindow(nil,'Form1');

if wnd<>0 then
begin
ssaa.single:= GetCurrentProcessID;
SendMessage(wnd ,my_definemsg,GetCurrentProcessID,Lparam(@ssaa));
end;
//生成监控信息 结束
end;
end;
end;

end.
 
写错题目了,是中止线程而不是进程,谢谢大家帮忙
 
差不多这样就可以。
procedure TForm1.Button2Click(Sender: TObject);
begin
if not unit2.test.Terminated then

unit2.test.Terminate
end;
不过你的整个例子写的有点问题,要注意数据同步问题

 
数据同步什么意思
我是初学线程的,请帮忙说一下
 
ssaa.single:= GetCurrentProcessID;
这句错了,应是 ssaa.single:= GetCurrentThreadID;
这样才得到线程号,对吧
请告诉我数据同步什么意思
我的程序要用到这些,谢谢了
 
编译是说Terminated没定义怎么回事??
 
请帮忙 ,急啊,谢谢了
 
接受答案了.
 
后退
顶部