Recent

Author Topic: [SOLVED] Troubles with function declaration.  (Read 3401 times)

CM630

  • Hero Member
  • *****
  • Posts: 1553
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Troubles with function declaration.
« Reply #30 on: February 19, 2025, 02:38:30 pm »
...
IMHO the StaticVisaEventHandler and VisaEventHandler are most logically methods for TVisaSession class, not TForm1.
Exactly, I applied @Remy's solution in the session.pas and the event is generated from within the session.
A working sample is attached in https://forum.lazarus.freepascal.org/index.php/topic,33447.msg546766.html#msg546766, reply #27.


Anyway, it does not work without Application.QueueAsyncCall(@PlotMethod,0);, but with it it is fine.

...
 Don't forget to call:
Code: Pascal  [Select][+][-]
  1. Application.RemoveAsyncCalls(Self);
on form close to ensure there isn't pending async calls at the time of the form destruction.
Is it wrong to call it inside PlotMethod? It seems to work fine this way:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PlotMethod(Data: PtrInt);
  2. var
  3.   WaveformData: AnsiString = '';
  4. begin
  5.   WaveformData := VisaSession.query('CURV?');
  6.   PlotWaveForm(WaveformData);
  7.   Application.RemoveAsyncCalls(Self);
  8. end;  
« Last Edit: February 19, 2025, 02:40:39 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: [SOLVED] Troubles with function declaration.
« Reply #31 on: February 19, 2025, 03:25:25 pm »
Quote
Anyway, it does not work without Application.QueueAsyncCall(@PlotMethod,0);, but with it it is fine.
As the name suggests, this will add the @{Self.}PlotMethod to the internal queue for execution. When the Application is into it's main GUI thread it will fetch from that queue and execute it asynchronously. The key phrase here is: "the main GUI thread". All of the GUI processing must be done in that thread!
Quote
Is it wrong to call it inside PlotMethod? It seems to work fine this way:
Calling Application.RemoveAsyncCalls(Self) will remove from the internal queue all previously added (but yet not realized) calls.They must be removed before destroying the object (ie. the red Self above) because otherwise they'll be invoked on a dangling pointer. See the first red {Self.} in QueueAsyncCall, which is implicit, but it's there!

So, I don't know if it is wrong to call it into PlotMethod (depends on the use case) but it's wrong not to call it just before destroying the PlotMethod's object instance.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: Troubles with function declaration.
« Reply #32 on: February 20, 2025, 09:42:56 pm »
Proc A and Proc B are just examples to make sure that I have understood reply #6, which unfortunately does not solve my problem.
But indeed I was not aware of the forward; statement, it seems comfortable.
Putting a procedure/function header into the unit interface section is the way to make them visible. The forward declaration is a remnant from TP3.0 days where no such thing as a unit was available.

It's not a remnant. It's necessary if you have functions that you don't want to publish, but that reference each other. Or if you have such a construct inside a program or library file. In addition to that forward is also part of ISO Pascal.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Troubles with function declaration.
« Reply #33 on: February 20, 2025, 10:25:50 pm »
It's not a remnant. It's necessary if you have functions that you don't want to publish, but that reference each other. Or if you have such a construct inside a program or library file. In addition to that forward is also part of ISO Pascal.

And in any case, it was in Pascal as defined by Wirth so is inviolate.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018