OnTimer event not work

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: OnTimer event not work

Postby rlebeau » Tue Mar 03, 2020 12:54 pm

mark_c wrote:If I start with socket ctNonBlocking and make the connection and once the connection is established I use the OnConnect event to close the socket ctNonBlocking and reopen a new one with ctBlocking could it work?


No. That is a completely separate connection, subject to the same networking rules, restrictions, and limitations as the previous connection. There is no guarantee that just because one connection succeeded then another connection will also succeed. They are treated independently.

Also, the code you presented will not work anyway, because you are exiting from the thread as soon as TClientSocket::Open() begins the connection process, before the OnConnect event can be fired. Remember, TClientSocket in non-blocking mode REQUIRES a message loop.

Also, your OnConnect code is leaking the original TClientSocket object when it creates a new object.

mark_c wrote:when the OnConnect event is generated it means that the Three-Way Hand Shake was successful and the connection was made correctly right?


Yes.

mark_c wrote:Sometimes it seems to me that even after the OnConnect event the connection does not actually work, as if there was no connection


It is possible that the connection was closed on the server itself, or maybe by a proxy/router sitting in between the client and server, after the handshake was complete.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: OnTimer event not work

Postby mark_c » Tue Mar 03, 2020 1:45 pm

sorry but I don't understand where exactly the TClientSocket :: Open () leaves the thread, when I call it for the second time in the OnConnect event?

Thank you
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: OnTimer event not work

Postby rlebeau » Tue Mar 03, 2020 8:09 pm

mark_c wrote:sorry but I don't understand where exactly the TClientSocket :: Open () leaves the thread, when I call it for the second time in the OnConnect event?


When ClientType is ctNonBlocking, TClientSocket::Open() is asynchronous. It begins the connection establishment and then exits immediately. The establishment is finished in the background, and then the OnConnect or OnError event is fired as needed when the establishment is done.

Thus, in this code you provided:

Code: Select all
void __fastcall TMyThread::Execute()
{
   MySock = new TClientSocket(NULL);

   MySock->Address = myadr;
   MySock->Port = StrToInt(Form1->Edit1->Text);
   MySock->ClientType = ctNonBlocking;
   MySock->OnConnect = &MySocketConnect;
   MySock->OnError = &MySocketError;
   
   MySock->Open(); // <-- EXITS IMMEDIATELY!!!
}


There is nothing after the Open() to prevent Execute() from exiting as soon as Open() exits. When Execute() exits, the thread is terminated.

As I told you earlier, when using ctNonBlocking, you MUST have a message loop, to not only keep the thread running, but also to allow the TClientSocket events to fire at all, as they are message-bound. Internally, TClientSocket in ctNonBlocking mode creates an internal HWND that WinSock sends socket activity to using window messages.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: OnTimer event not work

Postby mark_c » Wed Mar 04, 2020 12:49 am

sorry but I was convinced instead that the thread remained in memory and was terminated only by calling appropriate code.
While there are, since with the threads I still have some doubts, after the Open (), or use:

Code: Select all
__fastcall TMyThread::TMyThread(const AnsiString &Adr)
: TThread(true), myadr(Adr)
{
        FreeOnTerminate = true;
}


Code: Select all
or Terminate()


however, I was very convinced that it always remained in memory and that it was possible to reactivate it at any time with Resume().
I have to do more thread tests.

thanks Remy
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: OnTimer event not work

Postby rlebeau » Wed Mar 04, 2020 1:39 pm

mark_c wrote:sorry but I was convinced instead that the thread remained in memory and was terminated only by calling appropriate code.


At the OS level, a thread is terminated when its procedure calls the Win32 ExitThread() function. If the procedure exits without calling ExitThread(), the OS calls it implicitly for you.

For a TThread, its thread procedure (implemented by the RTL) calls your overriden Execute( method), and when that method exits, the procedure then fires the TThread::OnTerminate event if assigned, frees the TThread object if its FreeOnTerminate property is true, and then calls ExitThread().

mark_c wrote:While there are, since with the threads I still have some doubts, after the Open (), or use:

Code: Select all
__fastcall TMyThread::TMyThread(const AnsiString &Adr)
: TThread(true), myadr(Adr)
{
        FreeOnTerminate = true;
}


Code: Select all
or Terminate()


however, I was very convinced that it always remained in memory and that it was possible to reactivate it at any time with Resume().


Absolutely not. Whether you got that idea from, forget it, it is completely wrong.
Last edited by rlebeau on Fri Mar 06, 2020 5:51 pm, edited 1 time in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: OnTimer event not work

Postby mark_c » Fri Mar 06, 2020 11:00 am

Thanks Remy
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Previous

Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 26 guests