请问怎样在线程中传递过程或函数的参数。急。。。 ( 积分: 100 )

  • 主题发起人 主题发起人 silaszhe
  • 开始时间 开始时间
S

silaszhe

Unregistered / Unconfirmed
GUEST, unregistred user!
我继承了TThread类,构造了create。重载了execute。
在execute中写上我的函数。
请问我execute中函数的参数要怎么传递?全局变量?
比如:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure test(s:string);
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread=class(TThread)
protected
procedure Execute;
override;
public
constructor create;
end;

var
Form1: TForm1;
ttest: TMyThread;
implementation
{$R *.dfm}
constructor TMyThread.create;
begin
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TMyThread.Execute;
begin
//test();
不知道这样写行不行。如果可以test的参数应该怎么传递。
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ttest:=TMyThread.create;
end;

procedure TForm1.test(s: string);
begin
Showmessage(s);
end;

end.
 
我继承了TThread类,构造了create。重载了execute。
在execute中写上我的函数。
请问我execute中函数的参数要怎么传递?全局变量?
比如:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure test(s:string);
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread=class(TThread)
protected
procedure Execute;
override;
public
constructor create;
end;

var
Form1: TForm1;
ttest: TMyThread;
implementation
{$R *.dfm}
constructor TMyThread.create;
begin
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TMyThread.Execute;
begin
//test();
不知道这样写行不行。如果可以test的参数应该怎么传递。
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ttest:=TMyThread.create;
end;

procedure TForm1.test(s: string);
begin
Showmessage(s);
end;

end.
 
test('aaaaaaa');
如果需要外部传入参数,可以在重载Create
把参数作为Create的参数,然后把参数的值保存到此类的私有元素里
那么此类的其它方法就可以使用了
 
ak_2004您说的意思是不是在构造create的时候加上参数,比如
constructor create(s:string);
但用的时候怎么用 烦您给提点一下。
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Fstr : string;
procedure test(s:string);
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread=class(TThread)
protected
procedure Execute;
override;
public
constructor create(s:string);
overload;
end;

var
Form1: TForm1;
ttest: TMyThread;
implementation
{$R *.dfm}
constructor TMyThread.create(s:string);
begin
Fstr := s;
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TMyThread.Execute;
begin
test();
//不知道这样写行不行。如果可以test的参数应该怎么传递。
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ttest:=TMyThread.create;
end;

procedure TForm1.test();
begin
Showmessage(Fstr);
end;

end.
 
做为TMyThread的属性,TMyThread.create的时候先把线程挂起,对它的属性复制后再让它执行,Execute中访问的是TMyThread的属性。
 
ak_2004 不行啊。。
首先Fstr是TForm1的私有,TMyThread.Create(s:string)这个过程就过不去。我把它写成全局变量,可以用。
然后Button1.OnClick这个过程中 ttest:=TMyThread.Create 这句是不是需要带参数。
我改了之后变成这样,但还是有问题,程序还没运行起来就给我出错误。
Project Project1.exe raised exception class EInvalidOperation with message
'Canvasdo
es not allow drawing'. Process stopped.Use Step or Run to continue.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure test;
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread=class(TThread)
protected
procedure Execute;
override;
public
constructor create(s:string);
overload;
end;

var
Form1: TForm1;
ttest: TMyThread;
Fstr : string;
implementation
{$R *.dfm}
constructor TMyThread.create(s:string);
begin
Fstr := s;
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TMyThread.Execute;
begin
Form1.test;
//不知道这样写行不行。如果可以test的参数应该怎么传递。
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ttest:=TMyThread.create('a');
end;

procedure TForm1.test;
begin
Showmessage(Fstr);
end;

end.

烦请你再指教了
 
unit Unit1;

interface
uses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread=class(TThread)
Private
Fstr : string;
procedure test();
protected
procedure Execute;
override;
public
constructor create(s:string);
overload;
end;

var
Form1: TForm1;
ttest: TMyThread;
implementation
{$R *.dfm}
constructor TMyThread.create(s:string);
begin
Fstr := s;
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TMyThread.Execute;
begin
test();
//不知道这样写行不行。如果可以test的参数应该怎么传递。
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ttest:=TMyThread.create('aaa');
end;

procedure TMyThread.test();
begin
//Showmessage(Fstr);
这里用showmessage就有异常,但设断点调试没问题,运行就不行,我也不知道原因
messagebox(handle,'test',PChar(Fstr),0);
end;

end.
 
同意ak_2004,用constructor create(s:string);
overload;Right!
前两天刚用过,呵呵
 
多谢各位。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部