Am Donnerstag, 7. Juni 2007 17:11 schrieb Luca Olivetti: > En/na Luca Olivetti ha escrit: > > En/na Lee Jenkins ha escrit: > >> Hi all, > >> > >> I need to build a GUI application where a spawned thread will open > >> a socket, receive messages and the controls on the form > >> (TStringGride, etc) will need to be updated with values received > >> from the socket. > >> > >> Should I use a critical section for this? I remember that Delphi > >> has a method for interacting with the main thread from other > >> threads, is this supported in Laz/FPC? > > > > Synchronize(method) will execute method in the context of the main > > thread. > > Mmmh, I just made an experiment (under windows, but development will > probably follow under linux): I have a communication thread in a > continuous polling loop. Each time I complete a successful > communication, I increase a label tag and change its caption to > display the counter. > If I use synchronize to call the method of the main form and move the > window around, while I'm moving the window the counter (and so I > think the thread) stops
Yes, the thread stops, too. That's why I avoid synchonize. >, but if I don't use synchronize I can move > the window and see the label updating, so I wonder if synchronize is > really (always) needed nowadays. Yes it is. Of course, there are other ways to solve this, but you should not just ommit synchronize, especially when updating GUI controls. In your example, you could e.g. use InterlockedIncrement to inc your counter in the thread and update your label in mainthread either in application.OnIdle or with a timer, like: procedure TForm1.OnTimer(Sender : TObject); var LNewEvents : Longint; begin LNewEvents:=InterlockedExchange(ThreadSafeCounter,0); if LNewEvents > 0 then begin Label1.tag:=Label1.tag+LNewEvents Label1.caption:=inttostr(label1.tag); end; end; regards Burkhard _________________________________________________________________ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives