Autor Beitrag
Sina4ever
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Mo 14.08.06 12:57 
Hallo, kann mir jemand mal ein einfaches Thread beispiel geben...das Tutorial von Luckie oder eher gesagt die Beispiele sind mir noch zu kompliziert, oder einfach zu schwer zum lesen!

unter verwendung der vcl bitte!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 14.08.06 15:50 
Im Tutorial von Luckie gibt es auch ein letztes Kapitel, welches auf die Klasse TThread eingeht. Nicht immer ist das letzte auch das komplizierteste. Probier das mal aus - das ist recht einfach zu verstehen.

_________________
We are, we were and will not be.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Di 15.08.06 21:45 
ja verstehe das so halb, brauche noch nen ganz einfaches beispiel...
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 15.08.06 23:48 
Moin!

user profile iconSina4ever hat folgendes geschrieben:
ja verstehe das so halb, brauche noch nen ganz einfaches beispiel...

Also, du hast ein Tut, du hast da Beispiele, du hast auch was davon verstanden, dann würde ich sagen, du zeigst uns einfach mal deinen Code für einen eigenen Thread-Versuch und wir finden raus, was daran nicht OK ist, hm? ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Mi 16.08.06 18:21 
code bekomm ich noch nicht mal hin,


ich möchte eigentlich einfach nur ne procedure..sagen wir mal

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure holexmldaten;
var
temp : string;
begin
   try
      temp := idhttp1.get(url);
      //mache was mit den xmldaten und gebe sie aus, z.B. in einer Listbox
   finally
   //Wenn Fehler dann unterdrücken
   end;
end;


in einem Thread ausführen... Doch wo muss ich was hinpacken und wie muss ich was aufrufen damit ich das hinbekomme. evtl. wird die procedure auch mehrmals hintereinander aufgerufen...
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Do 17.08.06 09:11 
Guten Morgen :wink2:

user profile iconSina4ever hat folgendes geschrieben:
in einem Thread ausführen... Doch wo muss ich was hinpacken und wie muss ich was aufrufen damit ich das hinbekomme. evtl. wird die procedure auch mehrmals hintereinander aufgerufen...

ich kenne das besagte Tutorial nicht aber ich glaube kaum, dass so etwas grundlegendes nicht erklärt wird.

Du musst (wenn du die VCL Variante benutzt) eine Ableitung von TThread erstellen und dort die Methode Execute implementieren. In diese Methode packst du dann den Sourcecode der im Thread ausgeführt wird.

Such mal hier im Forum nach Suche in: Delphi-Forum TTHREAD UND EXECUTE.

Lg Martin

_________________
Ein Nutzer der Ecke ;-)
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: So 20.08.06 12:08 
ja das weiß ich, aber wo muss das ganze gesynct werden, etc. bei luckie um tut sind das so viele teile im code...
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 20.08.06 12:57 
Moin!

user profile iconSina4ever hat folgendes geschrieben:
ja das weiß ich, aber wo muss das ganze gesynct werden

Was willst du denn womit synchronisieren? :gruebel:

Hast du denn den Sinn von Synchronize() verstanden? ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: So 20.08.06 13:47 
ja damit ich die ausgabe in der vcl syncronisiere...also bitte macht mir doch mal ein beispielt!
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: So 20.08.06 13:48 
macht mir doch mal bitte ein projekt per vcl wo ich auf nen button klicke und ne procedure mit ausgabe in der vcl in einem thread gemacht wird...dann kann ich ja sehen wie ich was machen muss und schreibe es dann auf meine bedürfnisse um!
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 20.08.06 14:25 
Schau mal hier, vieleicht hilft Dir das weiter !?
hsg.region-kaisersla...hi/threads/index.php
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: So 20.08.06 14:55 
ok, hab jetzt mal was geschrieben, doch jetzt bekomme ich ne fehlermeldung mit idhttp protocol error:


Anwendung:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    IdHTTP1: TIdHTTP;
    procedure Button1Click(Sender: TObject);
  private
   Pieps : MeinThread;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Pieps := MeinThread.create(false);
 
end;

end.


und jetzt die Thread Unit:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
unit Unit2;

interface

uses
  Classes;

type
  MeinThread = class(TThread)
  private

  public
  procedure UpdateCaption;
  protected
    procedure Execute; override;
  end;

var
temp : string = '';



implementation

{ Wichtig: Methoden und Eigenschaften von Objekten in visuellen Komponenten dürfen
  nur in einer Methode namens Synchronize aufgerufen werden, z.B.

      Synchronize(UpdateCaption);

  und UpdateCaption könnte folgendermaßen aussehen:

    procedure MeinThread.UpdateCaption;
    begin
      Form1.Caption := 'Aktualisiert in einem Thread';
    end; }


{ MeinThread }

uses unit1;

procedure MeinThread.Execute;
begin
temp := form1.idhttp1.get('www.google.de');
Synchronize(UpdateCaption);


end;



procedure MeinThread.UpdateCaption;
    begin
      Form1.memo1.text := temp;
    end;
end.


Also was mache ich falsch?


Sina
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 20.08.06 15:32 
Moin!

Wenn du nur am Ende ein Ergebnis des Threads auswerten/verarbeiten willst, brauchst du Synchronize() nicht, das ist nur zur Laufzeit des Threads nötig. In diesem Fall würde ich dir das Ereignis OnTerminate empfehlen, das im Kontext des Hauptthreads ausgeführt wird:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure MyCallback(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  THttpThread = class(TThread)
  private
    FResult: String;
  public
    constructor Create(Callback: TNotifyEvent);
    procedure Execute; override;
    property Result: String read FResult;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor THttpThread.Create(Callback: TNotifyEvent);
begin
  inherited Create(FALSE); // direkt starten
  OnTerminate := Callback; // wenn fertig, das hier aufrufen
  FreeOnTerminate := TRUE; // Objekt freigeben, wenn fertig
end;

procedure THttpThread.Execute;
  var
    IdHttp: TIdHttp;
begin
  IdHttp := TIdHttp.Create(NIL);
  FResult := IdHttp.Get('http://www.google.de');
  IdHttp.Free;
end;

// -----------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear;
  THttpThread.Create(MyCallback);
  Memo1.Lines.Add('Start: '+IntToStr(GetTickCount));
end;

procedure TForm1.MyCallback(Sender: TObject);
begin
  Memo1.Lines.Add('Ende: '+IntToStr(GetTickCount));
  Memo1.Lines.Add(THttpThread(Sender).Result);
end;

end.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: So 20.08.06 16:08 
das verstehe ich jetzt wieder nicht, kannst du's anhand meines beispiels erklären?

ich brauche ja auch zur laufzeit des threads die synchronisation, das ist halt nur im beispiel nicht so, weil ichs einfach halten wollte...

aber wieso kann ich nicht auf idhttp zugreifen...oder wieso kommt da der fehler?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 20.08.06 23:25 
Moin!

user profile iconSina4ever hat folgendes geschrieben:
das verstehe ich jetzt wieder nicht

Sag mal, du hast Probleme mit user profile iconLuckies Tut ("bei luckie um tut sind das so viele teile im code"), du verstehst mein relativ einfaches Beispiel nicht, bist du sicher, dass Threadprogrammierung nicht vielleicht noch eine Spur zu hoch für dich ist... :? ;) (nicht böse sein, aber was willst du denn haben? c&p-fähigen Code? den gibt´s für Threads nicht...)

user profile iconSina4ever hat folgendes geschrieben:
kannst du's anhand meines beispiels erklären?

Das habe ich doch in meinem Beispiel getan, es tut doch genau das gleiche... :gruebel: OK, ich habe nicht Synchronize() benutzt, aber das da rein zu stricken, kann ja nicht wirklich ein Problem sein, oder? ;)

user profile iconSina4ever hat folgendes geschrieben:
aber wieso kann ich nicht auf idhttp zugreifen...oder wieso kommt da der fehler?

Du gibst in deinem Beispiel den Thread nicht wieder frei und die Http-Kompo verwendest du im Thread-Kontext, also mußt du die auch Thread-lokal anlegen.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Mo 21.08.06 14:11 
und wie lege ich sie im thread lokal an? und ich möchte das schon gern mit ner zweiten unit extra für den thread!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 21.08.06 14:14 
user profile iconNarses hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure THttpThread.Execute;
  var
    IdHttp: TIdHttp;

begin
  IdHttp := TIdHttp.Create(NIL);
  FResult := IdHttp.Get('http://www.google.de');
  IdHttp.Free;
end;

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Mo 21.08.06 14:24 
Danke und Sorry das ich nicht so einfach bin...aber will ja auch nur ein bischen dazulernen...Schau dir doch bitte noch mal beide Codeteile an, zu funktionieren scheints jetzt, evtl habe ich aber noch was falsch:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
   Pieps : MeinThread;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Pieps := MeinThread.create(false);
 
end;

end.


und die thread unit:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
unit Unit2;

interface

uses
  Classes,IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;

type
  MeinThread = class(TThread)
  private

  public
  procedure UpdateCaption;
  protected
    procedure Execute; override;
  end;

var
temp : string = '';



implementation



uses unit1;

procedure MeinThread.Execute;
  var 
    IdHttp: TIdHttp; 
begin 
  IdHttp := TIdHttp.Create(NIL); 
  temp := IdHttp.Get('http://www.google.de'); 
  IdHttp.Free;
  Synchronize(UpdateCaption);
end;



procedure MeinThread.UpdateCaption;
    begin
      Form1.memo1.text := temp;
    end;
end.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 21.08.06 14:28 
Moin!

Du gibst immer noch nicht den beendeten Thread frei. Entweder wie in meinem Beispiel im Konstruktor FreeOnTerminate auf TRUE setzen oder später manuell freigeben, aber so ist das ein Speicherleck. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sina4ever Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 76



BeitragVerfasst: Mo 21.08.06 14:34 
ok, danke narses, hab jetzt noch

pieps.FreeOnTerminate := true; hinzugefügt..

Kannst du mir jetzt noch sagen wie ich dem Thread, oder eher gesagt dem idhttp1.get eine url aus der Hauptform mit übergeben kann?

Danke Danke Danke, dann wars das auch :-)