Hello Arno,

> To make sure the events are triggered in worker thread context
> you must either create the TWSocket instance from worker thread's
> Execute method (suggested) or use methods ThreadDetach and
> ThreadAttach.

So, in this new architecture that I´m trying to make I will create the 
socket outside the thread, but than I use the ThreadAttach routine inside 
the thread´s Execute method. the Execute should be something like this:

procedure TDriverCommSocketThread.Execute;
begin

  if Assigned(FSocket) then
  begin

    //Ligar o socket ao thread
    FSocket.ThreadAttach;

    //Processar as mensagens
    FSocket.MessageLoop;

    //Desligar o socket do thread
    FSocket.ThreadDetach;

  end;

end;

FSocket is a pointer to socket passed to the thread´s constructor

But I think you have answered my question, so, if I use the ThreadAttach on 
the Execute method and the MessageLoop on the execute, so, the 
OnDataAvailable event will be triggered by the thread right? inside the 
thread context. So, by doing this I could have a lenghty operation, and this 
operation will not affect my main thread.


I´m thinking on one behavour:
I want to compare the behavour of 2 scenarios

First: I have a socket wich the messages are being processed by the main 
thread (The default operation of ICS) and a working thread that will consume 
the data received by the socket, so, the socket should receive the data and 
put it on a buffer, than, the working thread periodically reads the buffer, 
process the data and empty the buffer.

Second: I have a socket wich the messages are being processed by the working 
thread though the socket´s MessageLoop procedure.

In both cases, what would happen if the working thread has a very very 
lenghty operation (That could take a lot of seconds to complete) and the 
network traffic is much intense (Like 8Mbps) ?
Correct me if I´m wrong: What would happen on first scenario is that the 
main thread would be working normally receiving the network traffic and 
storing it an internal buffer that I created and as the working thread is 
still processing the old data, this buffer could became full in few time.
In the second, as the thread is working on the data (In the OnDataAvailable 
event), it should´t process any more messages, forcing windows winsock to 
notify the other side to stop sendind packets until it could process it 
again.

So, I think that if it is this that happens (I don´t really know if winsock 
works like this), the second scenario would be more appropriate as the 
network traffic could be suspended a little while the thread is processing 
the data.

I think that this may be a little confusing :)

Thanks!

Éric


----- Original Message ----- 
From: "Arno Garrels" <[EMAIL PROTECTED]>
To: "ICS support mailing" <twsocket@elists.org>
Sent: Monday, August 07, 2006 12:25 PM
Subject: Re: [twsocket] Multithreaded Client Application


Éric Fleming Bonilha wrote:
> So, one doubt that I have is, if I´m using a thread to process the
> socket
> messages using the MessageLoop procedure, all the TWSocket events like
> OnSessionConnected and OnDataAvailable is triggered using the thread
> context, right? So, my handle for those events should be working on
> the
> thread context?

To make sure the events are triggered in worker thread context
you must either create the TWSocket instance from worker thread's
Execute method (suggested) or use methods ThreadDetach and ThreadAttach.
To realize a poll in intervals you could write a message loop in Execute
by the help of Windows function MsgWaitForMultipleObjects().
Does this answer your question, I'm not sure whether I understood it
correctly?

For instance the Excecute method may look something like below.

CurTimeOut := 10000;
StartTicks := GetTickCount;
while not Terminated do
begin
    Res := MsgWaitForMultipleObjects(0,
                                     Dummy,
                                     False,
                                     CurTimeOut,
                                     QS_POSTMESSAGE or QS_SENDMESSAGE);
    // if there is one or more messages in the queue ...
    if Res = (WAIT_OBJECT_0 + 0) then
    begin
        while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
        begin
            case Msg.message of
                WM_QUIT : begin Terminate; Break; end;
                {more, user defined messages here}
                [..]
            else
                TranslateMessage(Msg);
                DispatchMessage(Msg);
            end;
        end;
        { Recalculate Timeout }
        CurTicksAppart := CalcTicksAppart(StartTicks, GetTickCount);
        if CurTicksAppart >= CurTimeOut then
        begin
            if not Terminated then
                DoSomething;
            StartTicks := GetTickCount;
            CurTimeOut := 10000;
        end
        else
            Dec(CurTimeout, CurTicksAppart);
   end
   else if Res = WAIT_TIMEOUT then
   begin
       if not Terminated then
           DoSomething;
       StartTicks := GetTickCount;
       CurTimeOut := 10000;
   end
end;



>
> ----- Original Message -----
> From: "Arno Garrels" <[EMAIL PROTECTED]>
> To: "ICS support mailing" <twsocket@elists.org>
> Sent: Monday, August 07, 2006 11:01 AM
> Subject: Re: [twsocket] Multithreaded Client Application
>
>
> Éric Fleming Bonilha wrote:
>> Hello,
>>
>> I´m writing a multi-threaded client application
>>
>> I read the ICS code and there is written that to make a real multi-
>> threaded application we should do ThreadAttach and use the
>> messageloop procedure on the execute method of the working thread.
>> I´m doing this, but, how do I stop this thread? How can I exit from
>> the loop to free the thread? There is written that the message
>> WM_QUIT should be sent, but to what handler?
>
> First of all, you don't need multi-threading by default for the socket
> I/O. ICS uses asynchron winsock API. If you want to process lengthy
> tasks the you should move that stuff in a workerthread and send the
> result when the thread finished.
>
>> There is written that the message
>> WM_QUIT should be sent, but to what handler?
>
> The message loop stopps, means function GetMessage() returns FALSE
> when it receives a WM_QUIT message. If yuo use TWSocket.MessageLoop
> you need to send it to the window handle (property Handle) of TWsocket
> (or property CtrlSocket).
>
> ---
> Arno Garrels [TeamICS]
> http://www.overbyte.be/eng/overbyte/teamics.html
>
>
>
>
>
> --
> To unsubscribe or change your settings for TWSocket mailing list
> please goto http://www.elists.org/mailman/listinfo/twsocket
> Visit our website at http://www.overbyte.be
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to