Jump to content
Skullcode

Twsocket Tcp Client

Recommended Posts

is it safe to access VCL Controls directly from Twsocket DataAvailable  event. or it needs to be synchronized ?

 

as example 

 

 

procedure Tchatu.wsocketDataAvailable(Sender: TObject; ErrCode: Word);
var
datastr : string;
begin

datastr := wsocket.ReceiveStrW(CP_UTF8);


memo1.lines.add(datastr);

end;

 

 

is it safe that way ?

 

 

Edited by Skullcode

Share this post


Link to post

Most ICS applications do not use threads so synchronise is not needed,  However receiving data is blocked while you do anything in the OnDataAvailable event so not a good idea to update a memo if you are expecting to receive a lot of data. 

 

Angus

 

Share this post


Link to post

can you guide me on how to update the memo safely in that case ? i am expecting to receive a lot of data 

Share this post


Link to post
40 minutes ago, Skullcode said:

is it safe to access VCL Controls directly from Twsocket DataAvailable  event. or it needs to be synchronized ?

I don't use ICS, so I don't know the answer to that.  However, an easy way to find out is to simply have the event handler compare the return value of the Win32 GetCurrentThreadId() function against the value of the RTL's MainThreadID variable.  If the values match, the event is run in the UI thread and accessing UI controls is safe, otherwise access needs to be synchronized.

12 minutes ago, Skullcode said:

can you guide me on how to update the memo safely in that case ? i am expecting to receive a lot of data 

If the TWSocket is running in the context of the main UI thread, then whether you do the logic in the OnDataAvailable event or in another UI event, you are still going to block subsequent data receiving while the TMemo is busy adding data.  I would say don't worry about it unless it proves to be a real bottleneck, in which case you can then either move the TWSocket to another thread that posts new data to the main UI thread asynchronously so as not to block the socket, or else perhaps put new data into a queue somewhere and then use a timer to periodically dump the queue into the TMemo.

  • Like 1

Share this post


Link to post

The main issue is you rarely want to put a lot of data into a memo, you can not view it while being received since it is updated so often, unless you only want to see the last few lines or are receiving data slowly, like alarm signals or something.  

 

The most efficient way to update a log window is to write data for display to a buffer (simple string is fine) and then use a timer in the application to empty the buffer to the TMemo every one to two seconds, which is about as often you can see it. 

 

That is exactly what more recent ICS samples do, like OverbyteIcsHttpRestTst1.pas, the AddLog proc builds the line and writes a file, the TimerLogTimer event updates the TMemo.   In some applications I simply discard most of the log lines if there are hundreds arriving each second.  

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×