Autor Beitrag
huhn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Fr 15.04.05 19:17 
Hi leute!
hab folgendes problem :ich hab ne art minichat gemacht mit nem thread der reagiert wenn ich nachrichten empfange!nun kommt jedoch immer eine fehlermeldung beim schließen der datei wenn ich noch verbunden bin(hab schon einiges aussprobiert,auch den thread wieder zu zerstören)
er will immer das hier haben:IdTCPConnection.pas
dann kommt ne fehlermeldung:

---------------------------
Benachrichtigung über Debugger-Exception
---------------------------
Im Projekt CBClient.exe ist eine Exception der Klasse EIdNotConnected aufgetreten. Meldung: 'Not Connected'. Prozess wurde angehalten. Mit Einzelne Anweisung oder Start fortsetzen.
---------------------------
OK Hilfe
---------------------------

und dann wieder das:IdTCPConnection.pas
was muss ich noch machen das er die verbindung trennt?
des einfache disconnect klappt irgendwie net und i weiß au net warum , i weiß nur das des mit dem thread zusammen hängt.
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:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
unit MainForm;

interface

uses
  Windows, Messages, Graphics, Controls, Forms, Dialogs, ToolWin, ComCtrls,
  Menus, Buttons, Spin, SysUtils, Classes, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, ExtCtrls, StdCtrls;

type
  TReadThread = class(TThread)
  public
    constructor Create(IndyClient: TIdTCPClient);
    destructor Destroy; override;
  private
    FIndyClient: TIdTCPClient;
    FMessage: String;
    procedure DisplayString;
  protected
    procedure Execute; override;
  end;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    edUserName: TEdit;
    Label2: TLabel;
    edServer: TEdit;
    Label3: TLabel;
    lbClients: TListBox;
    Label4: TLabel;
    memLines: TMemo;
    Label5: TLabel;
    edMessage: TEdit;
    SpeedButton1: TSpeedButton;
    IdTCPClient1: TIdTCPClient;
    Timer1: TTimer;
    Label6: TLabel;
    sePort: TSpinEdit;
    Button1: TButton;
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure edMessageKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    tr: TReadThread;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TReadThread }

constructor TReadThread.Create(IndyClient: TIdTCPClient);
begin
  FIndyClient := IndyClient;
  inherited Create(false);
end;

destructor TReadThread.Destroy;
begin
  findyclient.Free;
  inherited Destroy;
end;  

procedure TReadThread.DisplayString;
var com:string;
begin
   if FMessage <> '' then
    if FMessage[1] <> '@' then
      begin
      { Not a system command }
        form1.memLines.Lines.Add(FMessage);
      end
    else
      begin
      { System command }
        Com := UpperCase(Trim(Copy(FMessage, 2, Pos(':', FMessage) -2)));
        FMessage := UpperCase(Trim(Copy(FMessage, Pos(':', FMessage) +1, Length(FMessage))));
        if Com = 'CLIENTS' then
          form1.lbClients.Items.CommaText := FMessage;
      end;
  //Form1.Memlines.Lines.Add(FMessage);
end;

procedure TReadThread.Execute;
begin
  while not Terminated and FIndyClient.Connected do
  begin
    //FMessage := FIndyClient.ReadLn('.');
    FMessage :=findyclient.ReadLn('');
    Synchronize(DisplayString);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  width := width + 1;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if (edUserName.Text <> '') and
     (edServer.Text <> '')   and
     SpeedButton1.Down then
    begin
      IdTCPClient1.Host := edServer.Text;
      IdTCPClient1.Port := sePort.Value;
      if SpeedButton1.Down then
        IdTCPClient1.Connect;
    end
  else
    begin
      if (edUserName.Text = '') or
         (edServer.Text = '')   then
        ShowMessage('You must put in a User Name and server name to connect.');
      SpeedButton1.Down := false;
    end;
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
  IdTCPClient1.WriteLn(edUserName.Text);
  tr := TReadThread.Create(IdTCPClient1);
end;

procedure TForm1.edMessageKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    begin
      key:=#0;         //if text=leer dann stop!!1
      IdTCPClient1.WriteLn(edMessage.Text);
      edMessage.Text := '';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdTCPClient1.WriteLn('@' + 'CLIENTS:REQUEST');
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
  if IdTCPClient1.ClosedGracefully then
    Memlines.Lines.Add('[Disconnected gracefully]')
  else
    Memlines.Lines.Add('[Disconnected abruptly]');
  tr.Free;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  tr.Free;                             //hier beim schliesen disconnecten!
                                 //klappt aber einfach net mit dem 
                               //thread
  idtcpclient1.Disconnect;}
end;

end.


thx 4 reply!

mfg huhn

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
huhn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Fr 15.04.05 22:26 
es hängt irgendwiemit dem destructor zusammen,wenn ich ihn weglasse kommt keine fehlermeldung mehr aber das programm wartet noch immer auf nachricht vom server-->hat also noch nicht disconnected :(
weiß jemand wie ich des verschachtelte ding einfach disconnecten oder beenden kann ohne das sich das programm aufhängt oder eine fehlermeldung kommt?
es liegt halt irgendwie mit dem thread zusammen,der wartet(vielleicht kann mir einer sagen wie ich den thread sofort beende,ohne das er noch auf nachricht wartet?)
mfg huhn :cry: (viel zuviel fragen)

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
huhn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Sa 16.04.05 13:23 
Nach längeren hin und her und fehleranalyse hab ich es hinbekommen!(au nach x-tutorials)
weiß zwar net ganz warum aber er hat immer noch ein letztes signal beim thread beenden gebracuht!
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:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
unit MainForm;

interface

uses
  Windows, Messages, Graphics, Controls, Forms, Dialogs, ToolWin, ComCtrls,
  Menus, Buttons, Spin, SysUtils, Classes, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, ExtCtrls, StdCtrls;

type
  TReadThread = class(TThread)
  public
    constructor Create(IndyClient: TIdTCPClient);
  private
    FIndyClient: TIdTCPClient;
    FMessage: String;
    run:boolean;
    procedure DisplayString;
    procedure Execute; override;
  protected
    //procedure Execute; override;
  end;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    edUserName: TEdit;
    Label2: TLabel;
    edServer: TEdit;
    Label3: TLabel;
    lbClients: TListBox;
    Label4: TLabel;
    memLines: TMemo;
    Label5: TLabel;
    edMessage: TEdit;
    SpeedButton1: TSpeedButton;
    IdTCPClient1: TIdTCPClient;
    Label6: TLabel;
    sePort: TSpinEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure edMessageKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    tr: TReadThread;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TReadThread }

constructor TReadThread.Create(IndyClient: TIdTCPClient);
begin
  FIndyClient := IndyClient;
  run:=true;
  inherited Create(false);
end;

procedure TReadThread.DisplayString;
var com:string;
begin
   if FMessage <> '' then
    if FMessage[1] <> '@' then
      begin
      { Not a system command }
        form1.memLines.Lines.Add(FMessage);
      end
    else
      begin
      { System command }
        Com := UpperCase(Trim(Copy(FMessage, 2, Pos(':', FMessage) -2)));
        FMessage := UpperCase(Trim(Copy(FMessage, Pos(':', FMessage) +1, Length(FMessage))));
        if Com = 'CLIENTS' then
          form1.lbClients.Items.CommaText := FMessage;
      end;
  //Form1.Memlines.Lines.Add(FMessage);
end;

procedure TReadThread.Execute;
begin
  run:=true;
  while (not Terminated) and (FIndyClient.Connected) do
  begin
    //FMessage := FIndyClient.ReadLn('.');
    FMessage :=findyclient.ReadLn('');
    Synchronize(DisplayString);
  end;
  run:=false;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  width := width + 1;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if IdTCPClient1.Connected=true then IdTCPClient1.DisconnectSocket;
  if (edUserName.Text <> '') and
     (edServer.Text <> '')   and
     SpeedButton1.Down then
    begin
      IdTCPClient1.Host := edServer.Text;
      IdTCPClient1.Port := sePort.Value;
      if SpeedButton1.Down then
        IdTCPClient1.Connect;
    end
  else
    begin
      if (edUserName.Text = '') or
         (edServer.Text = '')   then
        ShowMessage('You must put in a User Name and server name to connect.');
      SpeedButton1.Down := false;
    end;
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
  IdTCPClient1.WriteLn(edUserName.Text);
  tr := TReadThread.Create(IdTCPClient1);
end;

procedure TForm1.edMessageKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    begin
      key:=#0;         //if text=leer dann stop!!1
      IdTCPClient1.WriteLn(edMessage.Text);
      edMessage.Text := '';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdTCPClient1.WriteLn('@' + 'CLIENTS:REQUEST');
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
  if IdTCPClient1.ClosedGracefully then
    Memlines.Lines.Add('[Disconnected gracefully]')
  else
    Memlines.Lines.Add('[Disconnected abruptly]');
  tr.Free;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if (IdTCPClient1.Connected=true) and (tr.run=true) then
  begin
    Tr.Terminate;
    IdTCPClient1.WriteLn('');
    tr.Free;
    IdTCPClient1.DisconnectSocket;
  end
else
  begin
    if IdTCPClient1.Connected=true then IdTCPClient1.DisconnectSocket;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if (IdTCPClient1.Connected=true) and (tr.run=true) then
  begin
    Tr.Terminate;
    IdTCPClient1.WriteLn('');
    tr.Free;
    IdTCPClient1.DisconnectSocket;
  end;
end;

mfg huhn

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 16.04.05 14:16 
da sieht man mal, dass man für ein topic doch nicht mehr als 1 person braucht :lol:
huhn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Sa 16.04.05 17:29 
Falsch jetzt wo du was geschrieben hast sinds schon wieder 2! :D
mfg huhn

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 16.04.05 17:34 
mich hast du aber net gebraucht :lol:
huhn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Sa 16.04.05 18:03 
ok stimmt!
wenn ihr mir aber geholfen hättet,hätte ich es schneller hinbekommen.
naja egal so hab ich wenigstens einiges über threads gelernt
mfg huhn

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*