Someone who knows Delhi Indy 10 Sockets ? the component TIdTCPServer. I have a service where instantiate multiple threads ( multithreading ) using TIdTCPServer to process requests from several clients ( tidTCPClient ) .
It's all working, no problem , but in the service ( the server) I have a screen where update a log and a list of connected users and log what they are doing and view this log in the memo main screen.
It turns out that being Multithreaded must use the Syncronize to update the memo that is in thread main . So far so ok right?
The memo will be updated in the event idTCPServerExecute component , only that what I have just as event argument is ( AContext : TIdContext ) where I caught the instance of the thread running to her can call the syncronize ?
wanted to do something like:
AContext.thread.Syncronize ( LogMemo ); // Will write in the memo on the main thread.
But I searched and not found the thread object. Does anyone know where is this object?
or how I could update the screen in this event without being in competition with other threads ?
procedure TfrmMainServer.TCPServerExecute(AContext: TIdContext);
var
Cmd : String;
begin
if AContext.Connection.IOHandler.Connected then
begin
If not AContext.Connection.IOHandler.InputBufferIsEmpty Then Begin
Cmd := AContext.Connection.IOHandler.ReadLn;
// This is not acceptable in this way :
memoFile.Lines.Add ('TESTING');
txtInfoLabel.Caption := 'Arquivo enviado';
End;
end;
end;
You can derive a class from Indy's TIdSync
or TIdNotify
class and override the TIdSync.DoSynchronize()
or TIdNotify.DoNotify()
method to do what you need.
Or, in recent Delphi versions, you can use the anonymous procedure version of the static TThread.Synchronize()
or TThread.Queue()
methods.
Examples of both approaches have been posted many times before on Embarcadero's forum, Indy's forum, StackOverflow, etc. Search around.