[Lazarus] Enqueuing a callback from a thread via other class - or am I overcomplicating this ?

Lukasz Sokol el.es.cr at gmail.com
Wed Jun 14 12:12:11 CEST 2017


Hi,

I have a TThread, that processes a T(Thread)List of TSomeClass objects continuously.

I want to have a callback in the TSomeClass, to which another class can put their procedure to call into,
but the callback needs to be 'thread safe' (because it can be accessed (read) by GUI related elements).

I tried like this:

TSomeClass = class

private
... // other variables/fields
public
  class var ProcessedCallback : TThreadMethod;
  var
   ... //other variavbles

  procedure Processed;
end;

procedure TSomeClass.Processed;
begin
  if Assigned(ProcessedCallback) then 
    ProcessedCallback();
  
end;


and in the TMyThread I am trying to do

var SomeClassList : TThreadList; // is declared private to the thread - only the thread manages the lifetime of the list members

procedure TMyThread.Execute;
var 
    SomeClassLockedList: TList;
begin
  i := 0;
  while not Terminated do
    begin
      if SomeClassList.Count = 0 then 
      begin
        sleep(10);  
        continue;
      end;
      SomeClassLockedList := SomeClassList.LockedList; // there are try/excepts around all here, but did not want to muddle the picture
      Queue(TSomeClass(SomeClassLockedList.Items[i]).Processed); 
      // but above here I'm getting an error : got 'untyped', expected 'procedure variable type of object;Register'.
      SomeClassList.UnlockList;
      i := i+1;
    end;
  
end;

// the above sort-of gives the gist of what I'm trying to achieve

What am I doing wrong ? 
Does the procedure to be enqueued have to be declared in the TThread header ?

Hope some of it makes (any) sense ...

Thanks in advance,

-L.



More information about the Lazarus mailing list