Thema: FreePascal Thread und TIDHTTP

Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#4

AW: Thread und TIDHTTP

  Alt 31. Okt 2011, 13:56
ist zwar ein seltsamer Eintrag, mach bei mir aber wenn wie beschrieben eingefügt keinen Ärger...
weil ich schon ewig mit den Indy's nicht mehr gemacht habe hab ich mir den Spass gemacht ...
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls;

type
  TCallBack = Procedure(Const Content:String) of object;
  TTestThread=Class(TThread)
  Private
    FS:String;
    FCallBack:TCallBack;
    procedure Showtext;
  public
    constructor Create( CreateSuspended : Boolean;CallBack:TCallBack );overload;
    destructor Destroy; override;
    Procedure Execute; override;
  End;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    procedure CallBack(const s: String);

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.CallBack(Const s:String);
begin
  Memo1.Lines.Add(s)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    TTestThread.Create(false,CallBack);
end;

{ TTestThread }



constructor TTestThread.Create(CreateSuspended: Boolean;CallBack:TCallBack);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := true;
  FCallBack := CallBack;
end;

destructor TTestThread.Destroy;
begin

  inherited;
end;

procedure TTestThread.Showtext;
begin
 if Assigned(FCallBack) then FCallBack(FS);
end;

procedure TTestThread.Execute;
var
IdHTTP1 : TIdHTTP;
ss:TStringStream;
begin
  inherited;
    ss:=TStringStream.Create;
    IdHTTP1:= TIdHTTP.Create(nil);
    try
    IdHTTP1.Get('http://www.google.de',ss);
    ss.Position := 0;
    FS := ss.ReadString(ss.Size);
    Synchronize(Showtext);
    finally
      ss.Free;
      IdHTTP1.Free;
    end;
end;

end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat