opc0de
opc0de

Reputation: 11768

TIdHttpServer synchronize with VCL

I am using TIdHttpServer to process some commands the problem is that some commands are beeing lost my guess is that it's from the fact that i am updating the vcl inside OnConnect.

How can i use the Synchronize method to safley update the VCL ?

Upvotes: 2

Views: 1717

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596497

Indy has its own TIdSync and TIdNotify classes for synchronizing with the main thread in synchronous and asychronous manners, respectively. Derive a new class from TIdSync and override its DoSynchronize() method, or derive from TIdNotify and override its DoNotify() method.

Update: note that these classes are largely deprecated nowadays in favor of Delphi's own static versions of the TThread.Synchronize() and TThread.(Force)Queue() methods, which do much the same work that TIdSync/TIdNotify were originally designed for back when TThread still only supported a non-static Synchronize().

Upvotes: 4

Martin James
Martin James

Reputation: 24857

It's easy enough, just call TThread.Synchronize() with whatever TThreadMethod you want to be called by the GUI thread. TThreadMethod is a parameterless procedure of object, but since the secondary thread making the call is blocked until after the synchronized method has been executed by the GUI thread, you can use TThread members in the synchronized method without any further protection.

Just for completenes, I should mention that there are several alternatives, all of which are better (even those I haven't tried because nothing could be worse). Indy has TIdSync and TIdNotify classes that you can derive from to carry variables and implement custom methods. Newer Delphi versions have TThread.Queue() and TThread.ForceQueue() that can use anonymous methods to generate closures (with captured variables) that can be executed by the GUI thread without blocking the calling thread. There is also the PostMessage() and PostThreadMessage() APIs - a comms system that has worked without change since D3/W95 and is sure to be available on Windows forever.

Upvotes: 1

Related Questions