Autor Beitrag
Passi077
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Win XP
D7 Pers
BeitragVerfasst: Fr 13.07.07 20:38 
Hi,

kaum habe ich ein Problem gelöst, kommt das nächste..

Mein erster Thread läuft soweit nun :) .. wenn ich allerdings einen 2. erstelle und starte, so hängt sich das Programm sofort auf und freezt. Sieht also nach einem Deadlock aus, allerdings sehe ich nicht wo dieser erzeugt wird..

Hier mein Quelltext:
Die Thread-Klasse:

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:
unit Unit2;

interface

uses
  Classes, sysutils, StdCtrls, SyncObjs;

type
  TDLThread = class(TThread)
  private
    { Private-Deklarationen }
    count: integer;
    GUI: TLabel;
  protected
    procedure Execute; override;
    procedure DoSync;
  public
    constructor Create(pGUI: TLabel);
  end;

implementation

{ TDLThread }

constructor TDLThread.Create(pGUI: TLabel);
begin
  GUI := pGUI;
  FreeOnTerminate := True;
  inherited Create(False);
  Suspend();
end;

procedure TDLThread.Execute;
begin
  { Thread-Code hier einfügen }
 while not terminated do
 begin
  count := count + 1;
  Synchronize(DoSync);
 end;
end;

procedure TDLThread.DoSync();
begin
  GUI.caption := inttostr(count);
end;

end.




Und das Programm:
ausblenden 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:
var
  dlthreads : array of tdlthread;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  dlthreads[0].Resume();
//dlthreads[1].Resume();
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  dlthreads[0].Suspend;
//dlthreads[1].Suspend;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  setlength(dlthreads, 2);
  dlthreads[0] := tdlthread.Create(label1);
  dlthreads[1] := tdlthread.Create(label2);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  dlthreads[0].Terminate();
//dlthreads[1].Terminate();
end;



Sobald ich den dlthreads[1] mit Resume starte, hängt sich das Programm sofort auf. Kann dann nur noch per STRG+F2 beenden werden.. lasse die Threads alle Suspended starten.

Ich hoffe, Ihr könnt mir helfen :)!

Grüße
Passi
Dunkel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 682

Mac OS X Snow Leopard
Xcode 3.1
BeitragVerfasst: Fr 13.07.07 21:17 
Hallo!

Mach es lieber so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
constructor TDLThread.Create(pGUI: TLabel);
begin
  inherited Create(TRUE);  
  GUI := pGUI;
  FreeOnTerminate := True;
end;


Wenn Du den Thread mit TRUE createst dann ist dieser automatisch suspended. Siehe auch Delphi-OH!

_________________
Ich streite einsam mich mit dieser Oberflächenwelt
Gutes sei ein löblich Brot von dem ich zehre - bis zum Tod [Das Ich - Im Ich]
Passi077 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Win XP
D7 Pers
BeitragVerfasst: Fr 13.07.07 21:42 
Ah, ok Danke für den Tipp!

Leider ändert das noch nichts an dem Problem, dass sich das Programm aufhängt :(
Dunkel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 682

Mac OS X Snow Leopard
Xcode 3.1
BeitragVerfasst: Fr 13.07.07 21:58 
user profile iconPassi077 hat folgendes geschrieben:
Ah, ok Danke für den Tipp!

Leider ändert das noch nichts an dem Problem, dass sich das Programm aufhängt :(

Ja, kann ich verifizieren.

Bei mir klappt es, wenn Du im Execute ein Sleep einbaust. In etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TDLThread.Execute;
begin
  { Thread-Code hier einfügen }
 while not terminated do
 begin
  sleep(1);
  count := count + 1;
  Synchronize(DoSync);
 end;
end;

Frag mich aber nicht warum das so ist...

_________________
Ich streite einsam mich mit dieser Oberflächenwelt
Gutes sei ein löblich Brot von dem ich zehre - bis zum Tod [Das Ich - Im Ich]
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 13.07.07 22:47 
Ist eigentlich ganz einfach ...

Beim Aufruf von Synchronize wird mit SendMessage eine Windows-Message an das Hauptfenster der TApplication (Application.Handle) gesendet. Wenn Du nun so häufig synchronisierst, dass die Hauptanwendung ständig nur noch Nachrichten behandelt, so kommt die Anwendung zu nix anderem mehr, da auch beim Bearbeiten anderer Nachrichten der Thread ja weiterhin Ereignisse produziert ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Passi077 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Win XP
D7 Pers
BeitragVerfasst: Sa 14.07.07 00:10 
Ah, ja, das klingt logisch :)

OK, dann wird sich das Problem automatisch beheben, sobald alles vollständig implementiert ist.

Danke für die Info!

Grüße
Passi