Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: Gui Aktualisierung verschiedener Komponenten via Threads

  Alt 19. Feb 2014, 15:43
Keine Panik, um den "asynchronen Mehrfachaufruf" brauchst du dir eigentlich keinen Kopf machen, solange du das nicht vollmüllst

Delphi-Quellcode:
unit EmbeddedThread;

interface

uses
  System.Classes;

type
  TSubject = class
  private type
    TSubjectData = record
      Value : Integer;
    end;

    TWorkThread = class( TThread )
    private
      FSubject : TSubject;
      FData : TSubjectData;
    protected
      procedure Execute; override;
      procedure QueueData;
    public
      constructor Create( ASubject : TSubject );
    end;
  private
    FWorkThread : TWorkThread;
    FData : TSubjectData;
    FOnChange : TNotifyEvent;
    procedure SetData( const Value : TSubjectData );
    procedure DoNotifyChange;
    function GetValue : Integer;
  public
    constructor Create;
    destructor Destroy; override;

    property Value : Integer read GetValue;
    property OnChange : TNotifyEvent read FOnChange write FOnChange;
  end;

implementation

{ TSubject.TWorkThread }

constructor TSubject.TWorkThread.Create( ASubject : TSubject );
begin
  inherited Create( False );
  FSubject := ASubject;
end;

procedure TSubject.TWorkThread.Execute;
var
  LStep : Integer;
begin
  inherited;
  while not Terminated do
  begin
    Sleep( 250 );

    if FData.Value = 100 then
      LStep := - 1
    else if FData.Value = 0 then
      LStep := 1;

    FData.Value := FData.Value + LStep;
    QueueData;
  end;
end;

procedure TSubject.TWorkThread.QueueData;
begin
  Queue(
      procedure
    begin
      FSubject.SetData( FData );
    end );
end;

{ TSubject }

constructor TSubject.Create;
begin
  inherited;
  FWorkThread := TWorkThread.Create( Self );
end;

destructor TSubject.Destroy;
begin
  FWorkThread.Free;
  inherited;
end;

procedure TSubject.DoNotifyChange;
begin
  if Assigned( OnChange ) then
    OnChange( Self );
end;

function TSubject.GetValue : Integer;
begin
  Result := FData.Value;
end;

procedure TSubject.SetData( const Value : TSubjectData );
begin
  FData := Value;
  DoNotifyChange;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat