| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri May 04, 2007 7:41 pm Post subject: TIdSync get variable from main thread |
|
|
Hello,
How to get data from main thread using TIdSync class? Sample I found does
not return any information back. And it doesn't looks thread safe to add
ReturnInfo variable and read information from it after Write method called.
type
TLogSync = class(TIdSync)
ReturnInfo: String;
private
fDirection: String;
fLine: String;
fIPAddress: String;
procedure DoSynchronize; override;
public
procedure Write(const ADirection, ALine, AIPAddress:
String);
end;
procedure TLogSync.DoSynchronize;
begin
// write fDirection, fLine, and fIPAddress to TRichEdit as
needed...
ReturnInfo:=.
end;
procedure TLogSync.Write(const ADirection, ALine, AIPAddress:
String);
begin
fDirection := ADirection;
fLine := ALine;
fIPAddress := AIPAddress;
Synchronize;
end; |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri May 04, 2007 10:10 pm Post subject: Re: TIdSync get variable from main thread |
|
|
<column.column (AT) gmail (DOT) com> wrote in message
news:463b462d (AT) newsgroups (DOT) borland.com...
| Quote: | it doesn't looks thread safe to add ReturnInfo variable and
read information from it after Write method called.
|
Yes, it is, because the worker thread will not be accessing the
ReturnInfo until after DoSynchronize() has exited, so the main thread
won't be using it anymore by that time.
| Quote: | procedure TLogSync.DoSynchronize;
begin
// write fDirection, fLine, and fIPAddress to TRichEdit as
needed...
ReturnInfo:=.
end;
|
You can access your necessary data the same way you are accessing the
TRichEdit. If you have a pointer to your form, then you can access
its members normally.
Gambit |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 05, 2007 8:10 am Post subject: Re: TIdSync get variable from main thread |
|
|
I know how to get ReturnInfo from main form - I use procedure pointer that
returns value to ReturnInfo.
procedure TLogSync.DoSynchronize;
begin
if assigned (ProceedRequest) then
TProceedRequest(ProceedRequest)(ReturnInfo,...);
end;
Is it good do like this:
procedure TPOS_TCP.AserverSocketExecute(AContext: TIdContext);
var
mystring:string
begin
LogSync:=TLogSync.Create;
LogSync.ProceedRequest:=ProceedRequestThr;
LogSync.write(....);
mystring:= LogSync.ReturnInfo; //I don't like this
....
LogSync.free;
end; |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 05, 2007 6:40 pm Post subject: Re: TIdSync get variable from main thread |
|
|
| Quote: | Is it good do like this:
procedure TPOS_TCP.AserverSocketExecute(AContext: TIdContext);
var
mystring:string
begin
LogSync:=TLogSync.Create;
LogSync.ProceedRequest:=ProceedRequestThr;
LogSync.write(....);
mystring:= LogSync.ReturnInfo; //I don't like this
...
LogSync.free;
end;
|
looks it is not safe to do like this because sometimes i have nonsens in
ReturnInfo.
Question: How to get data from main thread using TIdSync in
TidTCPserver.AserverSocketExecute(AContext: TIdContext); |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon May 07, 2007 10:18 pm Post subject: Re: TIdSync get variable from main thread |
|
|
<column.column (AT) gmail (DOT) com> wrote in message
news:463c34cb$1 (AT) newsgroups (DOT) borland.com...
| Quote: | mystring:= LogSync.ReturnInfo; //I don't like this
|
Why not? It works fine.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon May 07, 2007 10:20 pm Post subject: Re: TIdSync get variable from main thread |
|
|
<column.column (AT) gmail (DOT) com> wrote in message
news:463c8969 (AT) newsgroups (DOT) borland.com...
| Quote: | looks it is not safe to do like this
|
Yes, it is safe. DoSynchronize() is called in the context of the main
thread, not the worker thread. The worker thread will not be
accessing ReturnInfo until after DoSynchronize() has fully exited.
| Quote: | because sometimes i have nonsens in ReturnInfo.
|
Then you are doing something else wrong in your other code. The
TIdSync code itself that you have shown so far is fine.
| Quote: | How to get data from main thread using TIdSync in
TidTCPserver.AserverSocketExecute(AContext: TIdContext);
|
You already know the answer to that, because you are already doing it.
Gambit |
|
| Back to top |
|
 |
|