Forum > General

Is there something like crossplatform PostThreadMessage in Lazarus?

<< < (2/2)

stab:
Thanks Juha, for your suggestions and comments.

I realized my mistake with
private
    FThreadManager: TThreadManager;  instead of
protected
    FThreadManager: TThreadManager;

Still I think there is a point using ProcessMessages as I can thus logically threat corresponding actions in the main-thread instead of writing that code in a background-thread(even if the effect will be the same).

Have been writing Delphi-program since many years and know that variables are pointer-references but due to some other error I just didn't get it to work at first. Changed the code according to your suggestions and it worked att right.

I also changed TManagerClientThread to include a protected member
FName like:
TManagerClientThread = class(TThread)
  private
  protected
    FName : string;
    FThreadManager: TThreadManager;
  public
    constructor Create(CreateSuspended : boolean);
    constructor Create(ThreadManager : TThreadManager; Name : string; CreateSuspended : boolean); reintroduce;
    destructor Destroy; override;
  end;

with parameter Name added to the constructor

and

  TThreadManager = class(TObject)
  private
    FThreadList : TStringList;

    procedure Clear;
  public
    constructor Create;
    destructor Destroy; override;

    procedure RegisterThread(ThreadName : string);
    function PushMessage(ThreadName : string; ThreadMessageObject : TThreadMessageObject) : boolean;
    function PopMessage(ThreadName : string) : TThreadMessageObject;
  end;

In this way I don't need IntToStr(ThreadId) to find a threads queue

e.g:
function TThreadManager.PopMessage(ThreadName : string): TThreadMessageObject;
var
 ThreadQueue : TThreadQueue;
begin
  ThreadQueue :=
    TThreadQueue(FThreadList.Objects[FThreadList.IndexOf(ThreadName)]);
  if ThreadQueue <> nil then
    Result := ThreadQueue.PopMessage
  else
    Result := nil;
end;
 

Still a question, could the lookup be done in a faster way with your StringHashMap?

Staffan

JuhaManninen:

--- Quote from: stab on July 01, 2010, 02:49:36 pm ---Still I think there is a point using ProcessMessages as I can thus logically threat corresponding actions in the main-thread instead of writing that code in a background-thread(even if the effect will be the same).

--- End quote ---

TThread.Synchronize does that, too. See :
http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial#The_TThread_Class.
Both ways work.


--- Quote ---Still a question, could the lookup be done in a faster way with your StringHashMap?

--- End quote ---

Yes, little faster. It makes a noticeable difference only when you have many items there. Typically you don't have so many threads running at the same time.

Juha

Navigation

[0] Message Index

[*] Previous page

Go to full version