Autor Beitrag
stigge
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Do 30.10.08 13:32 
Hallo,
ich habe eine Anwendung mit einer GUI-Form und einem Thread. Das Erzeugen usw klappt ja.
Jetzt will ich aber einen TIdTCPServer in diesem Thread erstellen, was auch klappt.
Nur OnConnect, OnExecute, usw wird nicht ausgelöst.
Wie bekomme ich es hin, das sie ausgelöst werden?
delphiprogrammierer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Do 30.10.08 13:38 
Da du den Server im Thread On The Fly erzeugst musst du natürlich dem Ereignis OnConnect von TIdTCPServer eine Procedure zuweisen.



IDTCPServer.OnConnect := DoOnConnect ......
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Do 30.10.08 18:03 
EDIT:
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:
unit Unit1;

interface

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

type
  TWorkThread = class(TThread)
  var abcd: ttimer;
  procedure abcdTimer(Sender: TObject);
  protected
    procedure Execute; override;
end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  WorkThread: TWorkThread;

implementation

{$R *.dfm}

procedure TWorkThread.abcdTimer;
begin
  Form1.Label1.Caption := Form1.Label1.Caption + 'H';
end;

procedure TWorkThread.Execute;
begin
 abcd := ttimer.Create(nil);
 abcd.Interval := 500;
 abcd.OnTimer := abcdTimer;
 abcd.Enabled := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WorkThread := TWorkThread.Create(True);
  WorkThread.FreeOnTerminate := True;
  WorkThread.Resume;
end;

end.

So funktioniert, das Problem ist nur das sich das Label nicht ändert, aber warum? (Auf den Button hab ich natürlich geklickt)


Zuletzt bearbeitet von stigge am Do 30.10.08 18:12, insgesamt 1-mal bearbeitet
LexXis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 170
Erhaltene Danke: 3



BeitragVerfasst: Do 30.10.08 18:11 
//EDIT:
OK, hast dein Problem schon selber in den Griff bekommen. Um aus einem Thread heraus auf deine Form zugreifen zu können musst du die beiden synchronisieren. Sollte als Such-/Denkanstoß ausreichen ;)
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Do 30.10.08 18:30 
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:
unit Unit1;

interface

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

type
  TWorkThread = class(TThread)
  var abcd: ttimer;
  protected
    procedure Execute; override;
    procedure DoOnTimer(Sender: TObject);
     procedure Ausgabe;
end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  WorkThread: TWorkThread;

implementation

{$R *.dfm}

procedure TWorkthread.Ausgabe;
begin
  Form1.Label1.Caption := 'HJHH';
end;

procedure TWorkThread.DoOnTimer;
begin
  Synchronize(Ausgabe);
end;

procedure TWorkThread.Execute;
begin
 abcd := ttimer.Create(nil);
 abcd.Interval := 500;
 abcd.OnTimer := DoOnTimer;
 abcd.Enabled := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WorkThread := TWorkThread.Create(True);
  WorkThread.FreeOnTerminate := True;
  WorkThread.Resume;
end;

end.

Hab mir mal das angeschaut, passiert trotzdem nix auch wenn ich syncronisiere...
delphiprogrammierer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Fr 31.10.08 09:13 
Hoi,

es kann natürlich sein, dass der Thread schneller ist als 0,5 Sekunden und somit der Timer schon wieder destroyed ist.

Mach doch mal nach dem


abcd.Enabled := True;


ein

sleep ( 1000 );

Dann gehst du sicher, dass der Thread noch net vor dem Timer fertig ist.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 31.10.08 16:41 
Um das vielleicht auch noch kurz zu erklären:
Mit FreeOnTerminate teilst du deinem Thread mit, dass er sich selbst aus dem Speicher entfernen soll wenn er fertig ist. Der Thread läuft nur so lange wie die Methode Execute läuft. Bei dir ist diese aber sofort wieder beendet, denn es passiert ja nichts weiter darin, keine Warteschleife, etc., und damit wird der Thread sofort wieder beendet und aus dem Speicher entfernt.