Recent

Author Topic: [ASK] Synchronize and Queue with Parameters  (Read 4412 times)

Mujie

  • Jr. Member
  • **
  • Posts: 64
[ASK] Synchronize and Queue with Parameters
« on: September 01, 2015, 07:52:12 am »
Hi,

I try to pass parameter on my thread with Lazarus, but never succeed, please take a look at my codes

Code: [Select]
type
  ThreadDLL = class(TThread)
  protected
    procedure LoadingLib(const ProcName: ansistring; ProcQuery: array of string);
    procedure Execute; override;
  end;

  { Tmainform }

  Tmainform = class(TForm)
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

   procedure ThreadDLLExec;

var
  mainform: Tmainform;

implementation

type
  TDLL = function (ProcQuery: array of string; var DataResult: TMemoryStream): boolean; stdcall;


{$R *.lfm}

{ Tloginform }


procedure ThreadDLL.LoadingLib(const ProcName: ansistring; ProcQuery: array of string);
var
  MyHandle: TLibHandle;
  DLLFunc: TDLL;
  DataStatus: boolean;
  DataResult: TMemoryStream;
begin
  MyHandle := SafeLoadLibrary('myfunction.dll');
  if MyHandle<>0 then
    begin
      DLLFunc := TDLL(GetProcedureAddress(MyHandle,ProcName)); //ProcName 'GetSomething'
      if Assigned(DLLFunc) then
        begin
          DataStatus := DLLFunc(ProcQuery, DataResult);
          if (DataStatus = True) then
            begin
              //Whatever
            end
          else
            begin
              //Whatever;
            end;
        end;
    DataResult.Free;
    FreeLibrary(MyHandle);
    end
  else
    begin
      FreeLibrary(MyHandle);
      ShowMessage('myfunction.dll library not found');
    end;
  exit;
end;

procedure ThreadDLL.Execute;
begin
  if not Terminated then
    begin
      Synchronize(@LoadingLib); // <-- I want to pass paramaters here like ex. Synchronize(@LoadingLib(ProcName, ProcQuery))
      exit;
    end;
end;

procedure ThreadDLLExec;
var
 DLLExec: ThreadDLL;
begin
 DLLExec := ThreadDLL.Create(False);
 DLLExec.FreeOnTerminate := True;
 exit;
end;

............ more codes   

I want to pass paramaters at Synchronize(@LoadingLib) with Synchronize(@LoadingLib(ProcName, ProcQuery)) , but it not possible because Synchronize does'nt recognize it.                   

Then, I was found a good article about synchronize and queue with parameters here http://www.uweraabe.de/Blog/2011/01/30/synchronize-and-queue-with-parameters/. It was Delphi. Which we can use:

Code: [Select]
Synchronize(
      procedure
      begin
        CallMyProgress(PercentComplete);
      end);

My question is: It is possible at Lazarus? How?

I appreciate any help. Many thanks :)

balazsszekely

  • Guest
Re: [ASK] Synchronize and Queue with Parameters
« Reply #1 on: September 01, 2015, 08:11:48 am »
Quote
My question is: It is possible at Lazarus? How?
AFAIK both anonymous methods and Queue are supported in fpc 3.1.1(@marcov ?). You need to download fpc trunk.

PS: If you don't want to download/compile fpc 3.1.1,  as a workaround use two private variables: FProcName, FProcQuery for example.
One more thing: please avoid passing managed types to dll's. Although it's possible(see sharemem, fastsharemem etc...), usually is a recipe for disaster.
« Last Edit: September 01, 2015, 08:44:02 am by GetMem »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: [ASK] Synchronize and Queue with Parameters
« Reply #2 on: September 01, 2015, 08:47:41 am »
Quote
My question is: It is possible at Lazarus? How?
AFAIK both anonymous methods and Queue are supported in fpc 3.1.1(@marcov ?). You need to download fpc trunk.

Sven worked on anonymous methods afaik. He does most new Delphi language feature work, Sergei sometimes too.

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [ASK] Synchronize and Queue with Parameters
« Reply #3 on: September 01, 2015, 09:44:01 am »
PS: If you don't want to download/compile fpc 3.1.1,  as a workaround use two private variables: FProcName, FProcQuery for example.

@GetMem: I think I'll stick with private varible first.

Sven worked on anonymous methods afaik. He does most new Delphi language feature work, Sergei sometimes too.

@marcov: Good news, I'll used it on the future.

Thank you both.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: [ASK] Synchronize and Queue with Parameters
« Reply #4 on: September 01, 2015, 10:33:52 am »
At the moment anonymous methods are not supported yet, but some building blocks are in place. On a mac you are lucky: it's equivalent is supported in trunk (codeblocks).
Specialize a type, not a var.

 

TinyPortal © 2005-2018