unit TextPipeline;
interface
uses
Messages, Classes, SyncObjs, Windows, SysUtils;
const
WM_TEXTPIPEPLINE = WM_USER + 999;
type
ITextPipeline =
interface
procedure SetObserver(AHandle: THandle);
procedure Read(AList: TStrings);
procedure Write(AValue:
String);
end;
TTextPipeline =
class(TInterfacedObject, ITextPipeline)
constructor Create;
destructor Destroy;
override;
private
FSection: TCriticalSection;
FList: TStringList;
FObserver: THandle;
public
procedure SetObserver(AHandle: THandle);
procedure Read(AList: TStrings);
procedure Write(AValue:
String);
end;
TTestThread =
class(TThread)
constructor Create(AName:
String; APipeline: ITextPipeline);
private
FName:
String;
FPipepline: ITextPipeline;
protected
procedure Execute;
override;
end;
implementation
constructor TTextPipeline.Create;
begin
inherited Create;
FSection := TCriticalSection.Create;
FList := TStringList.Create;
end;
destructor TTextPipeline.Destroy;
begin
FSection.Free;
FList.Free;
inherited;
end;
procedure TTextPipeline.SetObserver(AHandle: THandle);
begin
FSection.Acquire;
FObserver := AHandle;
FSection.Release;
end;
procedure TTextPipeline.
Read(AList: TStrings);
begin
FSection.Acquire;
AList.AddStrings(FList);
FList.Clear;
FSection.Release;
end;
procedure TTextPipeline.
Write(AValue:
String);
begin
FSection.Acquire;
FList.Add(AValue);
if (FList.Count = 1)
and (FObserver <> 0)
then
PostMessage(FObserver, WM_TEXTPIPEPLINE, 0, 0);
FSection.Release;
end;
constructor TTestThread.Create(AName:
String; APipeline: ITextPipeline);
begin
inherited Create(False);
FName := AName;
FPipepline := APipeline;
end;
procedure TTestThread.Execute;
begin
while not Terminated
do
begin
FPipepline.
Write(IntToStr(GetTickCount) + '
' + FName);
Sleep(Random(50));
end;
end;
end.