Multithreading Delphi,如何从多线程向过程传递变量

Multithreading Delphi,如何从多线程向过程传递变量,multithreading,delphi,Multithreading,Delphi,我想从clMailMessage1SaveAttachment过程中访问myVariable,但我不知道如何访问 一些代码会很好,因为我想这比我的能力强。myVariable是WorkerThread()的本地变量,所以clMailMessage1SaveAttachment()无法看到它 如果TclMailMessage提供了一种关联用户定义数据的方法,则可以使用该方法传递指向myVariable的指针,例如: procedure TForm4.WorkerThread(Thread: TMu

我想从clMailMessage1SaveAttachment过程中访问myVariable,但我不知道如何访问


一些代码会很好,因为我想这比我的能力强。

myVariable
WorkerThread()
的本地变量,所以
clMailMessage1SaveAttachment()
无法看到它

如果
TclMailMessage
提供了一种关联用户定义数据的方法,则可以使用该方法传递指向
myVariable
的指针,例如:

procedure TForm4.WorkerThread(Thread: TMultiThreadThread;
  Parameters: TObject; var Data, Results: TObject);
var 
  clMailMessage1 : TclMailMessage;
  myVariable : string;
begin
  myVariable := 'A String from this Thread';
  clMailMessage1 := TclMailMessage.Create(self);
  clMailMessage1.OnSaveAttachment := clMailMessage1SaveAttachment;
  clMailMessage1.LoadMessage('myemail.eml'); 
  // The LoadMessage activates/calls the OnSaveAttachment procedure
  clMailMessage1.free;
end;

procedure TForm4.clMailMessage1SaveAttachment(Sender: TObject;
ABody: TclAttachmentBody; var AFileName: string; var AData: TStream;
var Handled: Boolean);
begin
  AData := TFileStream.Create(AFileName, fmCreate);
  // saves all attachments
  // **How do I access the myVariable here from the specific calling thread ?**
end;
否则,如果不选择定义用户定义的数据,则可以将
myVariable
移动到全局范围,并将其标记为,例如:

clMailMessage1.Tag := NativeInt(@myVariable);
...
myVariable := PString(TclMailMessage(Sender).Tag);

myVariable
WorkerThread()
的本地变量,因此
clMailMessage1SaveAttachment()
无法看到它

如果
TclMailMessage
提供了一种关联用户定义数据的方法,则可以使用该方法传递指向
myVariable
的指针,例如:

procedure TForm4.WorkerThread(Thread: TMultiThreadThread;
  Parameters: TObject; var Data, Results: TObject);
var 
  clMailMessage1 : TclMailMessage;
  myVariable : string;
begin
  myVariable := 'A String from this Thread';
  clMailMessage1 := TclMailMessage.Create(self);
  clMailMessage1.OnSaveAttachment := clMailMessage1SaveAttachment;
  clMailMessage1.LoadMessage('myemail.eml'); 
  // The LoadMessage activates/calls the OnSaveAttachment procedure
  clMailMessage1.free;
end;

procedure TForm4.clMailMessage1SaveAttachment(Sender: TObject;
ABody: TclAttachmentBody; var AFileName: string; var AData: TStream;
var Handled: Boolean);
begin
  AData := TFileStream.Create(AFileName, fmCreate);
  // saves all attachments
  // **How do I access the myVariable here from the specific calling thread ?**
end;
否则,如果不选择定义用户定义的数据,则可以将
myVariable
移动到全局范围,并将其标记为,例如:

clMailMessage1.Tag := NativeInt(@myVariable);
...
myVariable := PString(TclMailMessage(Sender).Tag);