Recent

Author Topic: Need help with Async function  (Read 2903 times)

nikel

  • Full Member
  • ***
  • Posts: 186
Need help with Async function
« on: December 19, 2018, 01:10:16 am »
Hi, I'm trying to call my async function but I'm getting error. Here's my code:

Code: Pascal  [Select][+][-]
  1. FCounter := FCounter+1;
  2. Application.QueueAsyncCall(@RunUpdaterAsync, FCounter); // Line 404

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync(Data: PtrInt);
  2. begin
  3.   RunShellExecute('updater.exe', '', 1);
  4. end;

This gives me error:
Quote
unit1.pas(404,50) Error: Incompatible type for arg no. 1: Got "<procedure variable type of procedure of object;Register>", expected "<procedure variable type of procedure(LongInt) of object;Register>"

How can I fix this?

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Need help with Async function
« Reply #1 on: December 19, 2018, 01:35:08 am »
Hi, I'm trying to call my async function but I'm getting error. Here's my code:

Code: Pascal  [Select][+][-]
  1. FCounter := FCounter+1;
  2. Application.QueueAsyncCall(@RunUpdaterAsync, FCounter); // Line 404

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync(Data: PtrInt);
  2. begin
  3.   RunShellExecute('updater.exe', '', 1);
  4. end;

This gives me error:
Quote
unit1.pas(404,50) Error: Incompatible type for arg no. 1: Got "<procedure variable type of procedure of object;Register>", expected "<procedure variable type of procedure(LongInt) of object;Register>"

How can I fix this?

your issue is from incompatible method signature.
replace your existing TForm1.RunUpdaterAsync(Data: PtrInt) method with the code below

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync;
  2. begin
  3.   RunShellExecute('updater.exe', '', 1);
  4. end;
  5.  

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Need help with Async function
« Reply #2 on: December 19, 2018, 02:22:00 am »
your issue is from incompatible method signature.
replace your existing TForm1.RunUpdaterAsync(Data: PtrInt) method with the code below

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync;
  2. begin
  3.   RunShellExecute('updater.exe', '', 1);
  4. end;
  5.  

Do you mean the example in the documentation is wrong? Because the OP's code is almost literally a copy from it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Need help with Async function
« Reply #3 on: December 19, 2018, 02:43:27 am »
How stupid of me! I didn't declare the procedure properly. I added the parameter where it was declared. This fixed the problem.

I'm new to pointers so can you help me? Do I need to free any pointers? Thanks for the replies.

EDIT: FCounter: PtrInt; Is this an integer or a pointer?
« Last Edit: December 19, 2018, 02:47:15 am by nikel »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Need help with Async function
« Reply #4 on: December 19, 2018, 07:24:53 am »
How stupid of me! I didn't declare the procedure properly. I added the parameter where it was declared. This fixed the problem.

I'm new to pointers so can you help me? Do I need to free any pointers? Thanks for the replies.
Yes you are suppose to free them manually
EDIT: FCounter: PtrInt; Is this an integer or a pointer?
it is a pointer sized integer ee 32bits on 32bits progams 64bits on 64bits programs.
« Last Edit: December 19, 2018, 07:32:48 am by HeavyUser »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Need help with Async function
« Reply #5 on: December 19, 2018, 08:54:43 am »
If you want to pass data to some function via QueueAsyncCall then take a look at this example:
https://forum.lazarus.freepascal.org/index.php/topic,38851.msg265181.html#msg265181
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Need help with Async function
« Reply #6 on: December 20, 2018, 12:14:02 am »
Thanks for the replies. I can't delete pointer. Dispose(FCounter) this gives me error:

Quote
Error: pointer type expected, but got "LongInt"

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Need help with Async function
« Reply #7 on: December 20, 2018, 12:29:54 am »
Thanks for the replies. I can't delete pointer. Dispose(FCounter) this gives me error:

Quote
Error: pointer type expected, but got "LongInt"

That's because it is not a pointer but a pointer-sized integer, i.e. an integer with the same size in bytes than a pointer. An integer.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018