INDY 10 TCP Server - Combine with Insecure VCL Code

VCL is not thread safe. Therefore, I think that it is not worth writing information in gui in the function INDY 10 TCP server.execute(...) .

How to send information from the server to VCL?

I need to change TBitmap inside the tcpserver.execute function. How to make this thread safe?

+4
source share
3 answers

Write material to the Indy VCL stream in the same way that you write material to the VCL stream from anywhere else. Common options include TThread.Synchronize and TThread.Queue .

Changing the standalone TBitmap does not require synchronization with the main stream. You can change it from any thread that you want, as long as you do this from only one thread at a time. You can use standard synchronization objects, such as critical sections and events, to ensure that only one thread uses it at a time.

+4
source

The best way to sync is to create and use a TidNotify descendant.

Define the tidnotify child and vcl proc, like this, with the corresponding private fields.

 TVclProc= procedure(aBMP: TBitmap) of object; TBmpNotify = class(TIdNotify) protected FBMP: TBitmap; FProc: TVclProc; procedure DoNotify; override; public constructor Create(aBMP: TBitmap; aProc: TVclProc); reintroduce; class procedure NewBMP(aBMP: TBitmap; aProc: TVclProc); end; 

then we implement it as follows

 { TBmpNotify } constructor TBmpNotify.Create(aBMP: TBitmap; aProc: TVclProc); begin inherited Create; FBMP:= aBMP; FProc:= aProc; end; procedure TBmpNotify.DoNotify; begin inherited; FProc(FBMP); end; class procedure TBmpNotify.NewBMP(aBMP: TBitmap; aProc: TVclProc); begin with Create(aBMP, aProc) do begin Notify; end; end; 

then from

 server.execute(...) 

name it like that

 procedure TTCPServer.DoExecute(aContext: TIdContext); var NewBMP: TBitmap; begin TBmpNotify.NewBMP(NewBMP, FVclBmpProc); end; 

If FVclBmpProcis is a private field that points to a procedure in a form that matches the signature of the TVclProc parameter. This field must be set via a property on the server object immediately after creation and before starting the server.

the method in the form will be free to use the resulting bitmap without fear of thread conflicts, deadlock and other nasty things created by accessing VCL controls without synchronization.

+2
source

One simple PostMessage (inside the stream) and message processing (outside the stream) were needed to create user interface updates ...

0
source

Source: https://habr.com/ru/post/1441618/


All Articles