?
Solved

Application is not closing because a thread is stuck in a repeat structure on the IdTCPClient.ReadLn procedure

Posted on 2003-03-30
10
Medium Priority
?
309 Views
Last Modified: 2010-04-04
Hey ppl,
I'm having a problem with my prog. It doesn't want to close because a thread is stuck in a repeat structure on the IdTCPClient.ReadLn procedure.

This is my code:

procedure TIrcClient.Execute;
begin
  repeat
    try
      if IrcSocket.Connected then ProcessIrcLine(IrcSocket.ReadLn(#13#10));
    except
      on E: Exception do begin
        ErrorLog('IrcClient,E.Message);
      end;
    end;
    sleep(10);
  until Terminated or Application.Terminated;
end;

procedure TIrcClient.Cancel;
begin
  Self.Terminate;
  IrcSocket.DisconnectSocket;
end;

On Application.Terminate; the prog doesnt close because the ircclient thread is still active.
When I run IrcClient.Terminate first the Execute procedure doesn't seem to end, so the Thread is still running. It's like the Terminated bool is not refreshed. Probably because of the Readln procedure in the repeat structure. I already tried adding a timeout to the readln procedure and as you can see I also tried adding an extra cancel procedure in the thread to Disconnect the socket before calling terminate :-\

The main program does close if i place the readln in a TTimer but thats not really working, I really want to use the repeat until structure.

Anybody got an idea how to solve this prob?
0
Comment
Question by:Jasper84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:Colin_Dawson
ID: 8234233
It's been a while since I used the indy components, but isn't there a variation on the ReadLn command that allows for a timeout?
0
 

Author Comment

by:Jasper84
ID: 8234359
"I already tried adding a timeout to the readln procedure"
0
 
LVL 6

Accepted Solution

by:
MannSoft earned 152 total points
ID: 8235198
If ReadLn is the problem, you either need to A) fix it or B) not use it.

Personally I would go with B.  The easiest way would be to buffer the text from the server yourself.  Example code based off yours:

procedure TIrcClient.Execute;
var
  Buf: String;
  Cmd: String;
begin
 Buf := '';
 repeat
   try
     if IrcSocket.Connected and IrcSocket.DataAvailable then
     begin
       Buf := Buf + IrcSocket.Read;
       while (Pos(#13#10, Buf) > 0) do
       begin
         Cmd := Copy(Buf, 1, Pos(#13#10, Buf) - 1);
         Delete(Buf, 1, Pos(#13#10, Buf) + 1);
         ProcessIrcLine(Cmd);  
       end;
     end;
   except
     on E: Exception do begin
       ErrorLog('IrcClient,E.Message);
     end;
   end;
   sleep(10);
 until Terminated or Application.Terminated;
end;


I dont use INDY so I dont know the function names (if they exist), but DataAvailable() should return true if there is text to be read from the input buffer, and Read() should read all the text from the input buffer.

It's all untested so there may be minor errors, but overall it should work fine.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jasper84
ID: 8235428
thx
0
 
LVL 4

Assisted Solution

by:Colin_Dawson
Colin_Dawson earned 148 total points
ID: 8235439
Just had another thought about this, maybe it's worth contacting the Indy Working group and ask them what to do?  They hang about somewhere on the borland community servers, check out they official site for more details on this http://www.indyproject.org/ They might also have an updated version of indy that solves this issue for you.

Col.
0
 

Expert Comment

by:JAPerrett
ID: 8237359
try the cancel procedure this way round

procedure TIrcClient.Cancel;
begin
 IrcSocket.DisconnectSocket;
 Self.Terminate;
end;

0
 
LVL 3

Expert Comment

by:smurff
ID: 8237746
listening
0
 

Expert Comment

by:CleanupPing
ID: 9316541
Jasper84:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question