Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-05

TThread.Synchronize from anywhere

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Damien Gerard

Hi !

I have to use threads in a GUI app.
I have a class which is descendant of TThread and Synchronize is used to update all Visual controls.
However most of the functions are not within this class and it is difficult to broadcast the current TThread var.

I would like to call TThread.Synchronize from anywhere but it requires a value for AThread :
class procedure Synchronize(AThread: TThread; AMethod: TThreadMethod);

May be `threadvar` could help me but I don't know if it is a good idea and if it is valid :

type
   TMyThread = class(TThread)
   protected
      procedure Execute;override;
      [...]
   end;
threadvar
   CurrentThread: TMyThread;
implementation
procedure TMyThread.Execute;
begin
   CurrentThread := Self;
   while not Terminated do
   begin
     // Loop
   end;
end;

Any advice ?

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat

Reply by email

Luca Olivetti
to Damien Gerard

En/na Damien Gerard ha escrit:

Show quoted text 7 lines
Hi ! I have to use threads in a GUI app. I have a class which is descendant of TThread and Synchronize is used to update all Visual controls. However most of the functions are not within this class and it is difficult to broadcast the current TThread var.

???
You are either calling these functions from a thread (so the thread is finally responsible for calling synchronize), or you're calling them from the main thread, so there's no need to call synchronize. OTOH if these functions are the one uptading the visual control, they should be called by the synchronized method, not by a normal function method. In any case there's no need for the function to know anything about the thread.
If the functions really need to know the thread, maybe they should be defined as methods of the thread or one of their parameters should be a TThread.

Bye

Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 Fax +34 93 5883007

Reply by email

Damien Gerard
to Luca Olivetti

Le May 10, 2008 ? 3:06 PM, Luca Olivetti a ?crit :

Show quoted text 21 lines
En/na Damien Gerard ha escrit: > Hi ! > > I have to use threads in a GUI app. > I have a class which is descendant of TThread and Synchronize is used > to update all Visual controls. > However most of the functions are not within this class and it is > difficult to broadcast the current TThread var. ??? You are either calling these functions from a thread (so the thread is finally responsible for calling synchronize), or you're calling them from the main thread, so there's no need to call synchronize. OTOH if these functions are the one uptading the visual control, they should be called by the synchronized method, not by a normal function method. In any case there's no need for the function to know anything about the thread. If the functions really need to know the thread, maybe they should be defined as methods of the thread or one of their parameters should be a TThread.

My thread calls functions to update a sqlite database. These updates may require a visual update.
Consequently I have to use Synchronize() to call these functions. So I would like to call synchronize only when I have to. However, I can not add a TThread parameter to all methods.

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat

Reply by email