线程同步

  TSynThread=class(TThread)
Buf:TStringList;
List :TStrings;
procedure AddDate;
constructor Create(CreateSuspended: Boolean);
procedure Execute; override;
procedure Terminate;
end;

{------------- TSynThread impl -----------------------------------------}
constructor TSynThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
Buf:= TStringList.Create;
end;

procedure TSynThread.Terminate;
begin
Buf.Free;
inherited;
end;

procedure TSynThread.Execute;
begin
Synchronize(AddDate);
end;

procedure TSynThread.AddDate;
begin
List.AddStrings(Buf);
end;

{-------------end TSynThread impl -----------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
DataThread: TSynThread;
begin
DataThread:=TSynThread.Create(True);
DataThread.List:=Memo1.Lines;
s:=DateTimeToStr(now);
DataThread.Buf.Add(s);
DataThread.Resume;
end;

 

你可能感兴趣的:(线程同步)