Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Mar : Re: TIdTCPServer - threads, global data
| Subject: | Re: TIdTCPServer - threads, global data |
| Posted by: | "Mathijs" (blab..@bla.hu) |
| Date: | 15 Mar 2006 08:27:44 |
15 mrt 2006, (Mathijs):
> Indy version 10.1.5.
>
> I use the server's onexecute to process data send by the clients.
>
> During the processing (in the connection-thread), I need to read and
> write from and to global objects. What's the best way to do this?
>
> Thanks in advance,
> M.
>
>
Hmm.. answering my own question... Oh, well. This is what looks safe to
me.
In the OnExecute, I create an object with the data I want to read and put
it in the Data property of the idContext (the connection-thread). Then I
send a message to the main form with a pointer to the idContext object.
Myonly concern is the
"while Assigned(AContext.Data) do Application.ProcessMessages;"
I need something like that, because the AContext.Data-object can't be
overwritten whithout being processed first (in the form's message
handler).
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var cmd: string;
begin
cmd:=AContext.Connection.IOHandler.ReadLn;
while Assigned(AContext.Data) do
Application.ProcessMessages;
AContext.Data:=TCommand.Create(cmd);
SendMessage(self.Handle,WM_USER+1,Integer(AContext),0);
end;
procedure TForm1.ExecuteInMainThread(var Msg: TMessage);
var cmd: string;
AContext: TidContext;
begin
AContext:=TidContext(Msg.WParam);
cmd:=TCommand(Acontext.Data).cmd;
TCommand(Acontext.Data).Free;
Acontext.Data:=nil;
//parse command
...
Thanks in advance for your comments and suggetions.