Einzelnen Beitrag anzeigen

scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#7

Re: Die erste Thread-Programmierung...

  Alt 31. Okt 2003, 22:12
Foldender Vorsachlag:

Delphi-Quellcode:
unit ThreadUnit;

interface

uses
  UrlMon, StdCtrls, ExtCtrls, Windows, SysUtils, Classes, Dialogs, Controls;

type
  TDownloadThread = class(TThread)
  private
       { Private declarations } 
     FStatusLabel : TLabel;
     BILDNAME, //
     URL_TEIL1, //
     URL_TEIL2, //Das sind die Texte von 5 LabeledEdits aus der Form1.
     VERZEICHNIS : TLabeledEdit; //Sie sollen im Thread verwendet werden (also müssen sie
     ANZAHL : integer; //übergeben werden (in Thread.Create), richtig?)
     procedure UpdateForm1;
  protected
     procedure Execute; override;
     procedure DownloadFiles;
  public
     { Public declarations }

     constructor Create(LEdit1,LEdit2,LEdit3,LEdit4,LEdit5 : TLabeledEdit; StatusLabel: TLabel); //stimmt das?
    end;

implementation

var
  z : integer;

{ Important: Methods and properties of objects in VCL or CLX can only be used
  in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TDownloadThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }
 

{ TDownloadThread } 

procedure TDownloadThread.UpdateForm1; //wie im obigen Standard-Beispiel!
begin
  FStatusLabel.Caption := 'Datei ' + IntToStr(z) + ' von ' + IntToStr(ANZAHL) + ' fertig.'; //geht so
end;


procedure TDownloadThread.DownloadFiles; // Hier hast du nur das TDownloadThread. vergessen, deswegen die Bezeichner-Fehler

var URL, ORT : string;
begin
z := 1;

if ForceDirectories(Verzeichnis.Text) then
while z <= ANZAHL do begin //hier kennt Delphi die
                     URL := URL_TEIL1.Text + InttoStr(z) + URL_TEIL2.Text; //BEZEICHNER nie!
                     ORT := VERZEICHNIS.Text + BILDNAME.Text + IntToStr(z) + '.jpg'; //weshalb? wo müssen sie
                                                                           //deklariert werden?
                     UrlDownloadtoFile(nil, PCHAR(URL), PCHAR(ORT), 0, nil);//sollen ja aus der
                                                                           //Form1 kommen (Edits)

                     Synchronize(UpdateForm1); //hab ich nach dem obigen Standard-Beispiel gemacht!
                     inc(z);
                     end;
end;



constructor TDownloadThread.Create(LEdit1, LEdit2, LEdit3, LEdit4, LEdit5 : TLabeledEdit; StatusLabel: TLabel);
begin
  inherited Create(false);
  
  URL_TEIL1 := LEdit1; //Kann man das hier so zuordnen?!
  URL_TEIL2 := LEdit2;
  ANZAHL := StrToInt(LEdit3.Text); //Es sollen eine best. Anzahl Bilder gedownloadet werden
  VERZEICHNIS := LEdit4;
  BILDNAME := LEdit5; //Benennung der geladenen Bilder (+fortlaufende Nr.)
  FStatusLabel := StatusLabel; // Hier wird quasi ein Pointer auf Form1.Label1 erstellt
  FreeOnTerminate := True;
end;



procedure TDownloadThread.Execute;
begin
  { Place thread code here } 
  DownloadFiles;
end;

end.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ThreadUnit;

type
  TForm1 = class(TForm)
    Button1: TButton;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    LabeledEdit4: TLabeledEdit;
    LabeledEdit5: TLabeledEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  DownloadThread : TDownloadThread;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
// so wird der Thread generiert (in Unit1):

DownloadThread := TDownloadThread.Create(
                       LabeledEdit1,
                       LabeledEdit2,
                       LabeledEdit3,
                       LabeledEdit4,
                       LabeledEdit5,
                       Label1);

end;

end.
Habe dabei keine Syntax-Fehler bekommen, das ausführen des Threads klappt auch.

[edit]
Sorry, waren ein paar Flüchtigkeitsfehler drin.
[/edit]
  Mit Zitat antworten Zitat