怎么样在线程中操作另一个FORM中的组件呢?
我的原码如下:
unit UPop3Thread;interfaceuses
Classes,UMailIndex,IdMessageClient ,IdMessage, CustomMsg, ComCtrls, SysUtils, Dialogs;type
Pop3Thread = class(TThread)
private
{ Private declarations }
function NewMail(MsgID: string; MailList: TStringList): boolean;
function CheckNewMail(MailList: TStringList; IdMsg: TIdMessage;MailCount: integer): integer;
protected
procedure Execute; override;
procedure ShowLabel1;
procedure ShowLabel2;
procedure ShowLabel3;
procedure ShowLabel4;
end;
implementation
uses
UPOP,Umain;
var
MailCount: integer;
NewMailCount: integer;
//FPOP: TFPOP;
//PersonRec: TPersonRec;
MailIndex: TMailIndex;
RecordStream: TRecordStream;
RecordStream1: TRecordStream;{ 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 Pop3Thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ Pop3Thread }procedure Pop3Thread.Execute;
var
i: integer;
ListItem: TListItem;
begin
FreeOnTerminate :=true;
//onTerminate:=FPOP.Create;
try
FPOP.IdPOP31.Connect;
MailCount:=FPOP.IdPOP31.CheckMessages;
Synchronize(ShowLabel3);
//FPOP.Label1.Caption :='搜索' + CustomName + '...';
//FPOP.Label2.Caption :='共有'+ IntToStr(CheckNewMail(MailList,IdMsg,MailCount)) + ' 新邮件' ;
Synchronize(ShowLabel1);
for i:=1 to MailCount do
........
在FORM中调用线程时运行是出错,
请问:是什么原因呢?怎么解决?
我的原码如下:
unit UPop3Thread;interfaceuses
Classes,UMailIndex,IdMessageClient ,IdMessage, CustomMsg, ComCtrls, SysUtils, Dialogs;type
Pop3Thread = class(TThread)
private
{ Private declarations }
function NewMail(MsgID: string; MailList: TStringList): boolean;
function CheckNewMail(MailList: TStringList; IdMsg: TIdMessage;MailCount: integer): integer;
protected
procedure Execute; override;
procedure ShowLabel1;
procedure ShowLabel2;
procedure ShowLabel3;
procedure ShowLabel4;
end;
implementation
uses
UPOP,Umain;
var
MailCount: integer;
NewMailCount: integer;
//FPOP: TFPOP;
//PersonRec: TPersonRec;
MailIndex: TMailIndex;
RecordStream: TRecordStream;
RecordStream1: TRecordStream;{ 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 Pop3Thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ Pop3Thread }procedure Pop3Thread.Execute;
var
i: integer;
ListItem: TListItem;
begin
FreeOnTerminate :=true;
//onTerminate:=FPOP.Create;
try
FPOP.IdPOP31.Connect;
MailCount:=FPOP.IdPOP31.CheckMessages;
Synchronize(ShowLabel3);
//FPOP.Label1.Caption :='搜索' + CustomName + '...';
//FPOP.Label2.Caption :='共有'+ IntToStr(CheckNewMail(MailList,IdMsg,MailCount)) + ' 新邮件' ;
Synchronize(ShowLabel1);
for i:=1 to MailCount do
........
在FORM中调用线程时运行是出错,
请问:是什么原因呢?怎么解决?
//FPOP.Label2.Caption :='共有'+ IntToStr(CheckNewMail(MailList,IdMsg,MailCount)) + ' 新邮件' ;不能这么写在上面的过程里。应写在Synchronize(ShowLabel3);procedure TPop3Thread.showlabel3;
begin
FPOP.Label1.Caption :='搜索' + CustomName + '...';
....
end;
然后定义个procedure LblCaption;在procedure 中显示Label的caption;
在函数constructor create(var lbl:TLabel)中将主线程的Label1做参数传递过来;
在该线程的 Execute中,调Synchronize(LblCaption)即可;