Article View: borland.public.cppbuilder.internet.socket
Article #3538Re: Threaded Client Application
From: Remy Lebeau (TeamB)
Date: Mon, 22 Aug 2005 16:54
Date: Mon, 22 Aug 2005 16:54
58 lines
3298 bytes
3298 bytes
"Curt" <noemailaddress> wrote in message news:43091119$1@newsgroups.borland.com... > In Form1->OnCreate() Do not use the OnCreate event in C++. It is a Delphi remnant that produces illegal behavior in C++ because it can be triggered before the constructor. > I instantiate the TIdThread by calling TIdThread * ClientThread = new TIdThread(); That will not work. Like TThread, TIdThread is a base class that you need to derive a new class from in order to write the code that the thread will actually run. TIdThread by itself does not do anything. In fact, TIdThread is an abstract class, so you cannot instantiate it directly anyway. > Then, within ClientThread, how should I use BeforeExecute, BeforeRun, > AfterRun, AfterExecute ? Where should I put the Connect() and > Disconnect() Use BeforeRun() to Connect() and AfterRun() to Disconnect(). Use Run() to read a packet of data. > where should I check to make sure that the connection has not been broken? Put an try..catch block inside of Run(). If the connection is broken, reading from the client socket will thrown an exception. As long as an exception does not escape from Run(), the thread will continue running. > how do I break out of the Run() loop without terminating the thread ? The only way is to call the thread's Stop() method. There is a StopMode property that tells the thread what to do when stopped. If set to smTerminate, the thread terminates itself. If set to smSuspend, the thread suspends itself. In the latter case, you would have to Start() the thread again at a later time from another thread, including when shutting the thread down for good. On the other hand, if you don't want the thread to suspend itself, just to go back to BeforeRun() and start over, then you can override the DoStopped() method (or use the OnStopped event) to call the thread's Start() method. Then the thread will not suspend itself at all. > I also thought of connecting in BeforeRun and then spawning an > additional thread in Run() You do not need another thread. You are already in a thread. > which would do the work of reading the Connection and then terminate > itself when a disconnect takes place returning control to the original thread > which would reestablish the connection. You do not need to do that. Gambit
Technical Details
Message-ID:
<430a0326$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsread.com!newsprint.newsread.com!63.218.45.10.MISMATCH!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<43091119$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,41952c36e9beb1f7 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <43091119$1@newsgroups.borland.com> Subject: Re: Threaded Client Application Date: Mon, 22 Aug 2005 09:54:21 -0700 Lines: 60 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <430a0326$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1124729638 68.69.17.41 (22 Aug 2005 09:53:58 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsread.com!newsprint.newsread.com!63.218.45.10.MISMATCH!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail