Delphi-Quellcode:
interface
uses
Classes, SyncObjs;
type
TInputMethode = procedure(AValue: string) of object;
TStringPuffer = class(TCriticalSection)
constructor Create;
destructor Destroy; override;
private
FList: TStringList;
public
function Count: Integer;
procedure Put(AValue: string);
function Get: string;
end;
TInputThread = class(TThread)
constructor Create(APuffer: TStringPuffer);
private
FPuffer: TStringPuffer;
protected
procedure Execute; override;
end;
TInput = class(TPersistent)
constructor Create;
destructor Destroy; override;
private
FInpThread: TInputThread;
FPuffer: TStringPuffer;
public
property Puffer: TStringPuffer read FPuffer;
end;
implementation
{ TInputThread }
procedure TInputThread.Execute;
var
Daten: string;
begin
// while not Terminated do;
Daten := 'test';
FPuffer.Put(Daten);
end;
constructor TInputThread.create(APuffer: TStringPuffer);
begin
inherited create(False); // CreateSuspended = False
freeOnTerminate := False;
end;
{ TInput }
constructor TInput.Create;
begin
inherited;
FPuffer := TStringPuffer.Create;
FInpThread := TInputThread.Create(FPuffer);
end;
destructor TInput.Destroy;
begin
FInpThread.Free;
FPuffer.Free;
inherited;
end;
{ TStringPuffer }
function TStringPuffer.Count: Integer;
begin
Enter;
try
Result := FList.Count;
finally
Leave;
end;
end;
constructor TStringPuffer.Create;
begin
inherited;
FList := TStringList.Create;
end;
destructor TStringPuffer.Destroy;
begin
FList.Free;
inherited;
end;
function TStringPuffer.Get: string;
begin
Enter;
try
if FList.Count = 0 then
Result := ''
else
begin
Result := FList[0];
FList.Delete(0);
end;
finally
Leave;
end;
end;
procedure TStringPuffer.Put(AValue: string);
begin
Enter;
try
FList.Add(AValue);
finally
Leave;
end;
end;
Der Hauptthread muss dann halt hin und wieder schaun ob neue Daten da sind.
Alternativ könnte der Puffer auch eine Nachricht per Post versenden, wenn neue Daten eintreffen.
Dafür habe ich hier im Forom schon mal eine Lösung geschrieben, also schau mal dort nach.