Recent

Author Topic: LAMW and TThread  (Read 2439 times)

Robert Gilland

  • Full Member
  • ***
  • Posts: 151
Re: LAMW and TThread
« Reply #15 on: June 25, 2019, 08:53:11 am »
here is the core code:

Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: C:\adt32\eclipse\workspace\asyncdemo\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Laz_And_Controls, AndroidWidget, user_service_intf,
  10.   user_service_intf_proxy, And_Log_h;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.       jAsyncTask1: jAsyncTask;
  18.       jButton1: jButton;
  19.       jDialogProgress1: jDialogProgress;
  20.       jEditText1: jEditText;
  21.       jListView1: jListView;
  22.       procedure AndroidModule1Create(Sender: TObject);
  23.       procedure AndroidModule1Destroy(Sender: TObject);
  24.       procedure jAsyncTask1DoInBackground(Sender: TObject; progress: Integer;
  25.         out keepInBackground: boolean);
  26.       procedure jAsyncTask1PostExecute(Sender: TObject; progress: integer);
  27.       procedure jAsyncTask1PreExecute(Sender: TObject; out
  28.         startProgress: integer);
  29.       procedure jButton1Click(Sender: TObject);
  30.     private
  31.       {private declarations}
  32.       procedure LogMessage( psMsg : String );
  33.       procedure   RunGetUsers;
  34.       function CreateUserProxy : UserService;
  35.     public
  36.       {public declarations}
  37.       slUsers : TStringList;
  38.   end;
  39.  
  40. var
  41.   AndroidModule1: TAndroidModule1;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. uses soap_formatter, json_formatter, xmlrpc_formatter, fpc_http_protocol, service_intf;
  48.  
  49. { TAndroidModule1 }
  50.  
  51.  
  52. procedure TAndroidModule1.jAsyncTask1DoInBackground(Sender: TObject; progress: Integer; out keepInBackground: boolean);
  53. begin
  54.   RunGetUsers;
  55.   keepInBackground:= False;
  56. end;
  57.  
  58. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  59. begin
  60.   slUsers := tStringList.Create;
  61. end;
  62.  
  63. procedure TAndroidModule1.AndroidModule1Destroy(Sender: TObject);
  64. begin
  65.   slUsers.Free;
  66. end;
  67.  
  68. procedure TAndroidModule1.jAsyncTask1PostExecute(Sender: TObject; progress: integer);
  69. begin
  70.   LogMessage('Dialog Stop');
  71.   jDialogProgress1.Stop;
  72.   LogMessage('Async Stop');
  73.   jAsyncTask1.Done;
  74.   LogMessage('List View Assign');
  75.   jListView1.Items.Assign(slUsers);
  76.   ShowMessage(slUsers.text);
  77. end;
  78.  
  79. procedure TAndroidModule1.jAsyncTask1PreExecute(Sender: TObject; out startProgress: integer);
  80. begin
  81.    startProgress:= 0;  //out param !
  82.    LogMessage('Dialog Start');
  83.    jDialogProgress1.Start; //or Show
  84. end;
  85.  
  86.  
  87.  
  88.  
  89. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  90. begin
  91.  
  92.    if not jAsyncTask1.Running then
  93.    begin
  94.      //ShowMessage('Execute ... ');
  95.      jAsyncTask1.Execute
  96.    end
  97.   else
  98.      ShowMessage('Running...');
  99. end;
  100.  
  101. procedure TAndroidModule1.LogMessage(psMsg: String);
  102. begin
  103.   __android_log_write(ANDROID_LOG_INFO,PChar(Text),PChar(psMsg));
  104. end;
  105.  
  106. procedure TAndroidModule1.RunGetUsers;
  107. var
  108.   lpUserService : UserService;
  109.   loUserArray   : TUserArray;
  110.   loUser        : TUser;
  111.   i             : Integer;
  112.  
  113. begin
  114.   LogMessage('Create proxy');
  115.   lpUserService := CreateUserProxy;
  116.   LogMessage('proxy Get List');
  117.   loUserArray   := lpUserService.GetList();
  118.   slUsers.Clear;
  119.   LogMessage('List Length ' + IntToStr(loUserArray.Length));
  120.   for i := 0 to loUserArray.Length-1 do
  121.     begin
  122.       loUser := loUserArray[i];
  123.       slUsers.Add( loUser.UserName );
  124.     end;
  125. end;
  126.  
  127. function TAndroidModule1.CreateUserProxy: UserService;
  128. var
  129.   buffsimplecall, buffTransport, buffFormat : string;
  130.  
  131.   function GetServiceURL( WebServiceAddress : String ):String;
  132.   begin
  133.     Result := WebServiceAddress   + '/services/UserService';
  134.   end;
  135.  
  136. begin
  137.   buffFormat     := 'json';
  138.   buffsimplecall := 'http:Address=' + GetServiceURL(jEditText1.Text) + '/?format=' + buffFormat;
  139.   buffTransport  := buffsimplecall;
  140.   buffFormat := buffFormat + ':';
  141.   LogMessage(buffTransport);
  142.   LogMessage(buffFormat);
  143.   Result     := TUserService_Proxy.Create( 'UserService',  buffFormat,  buffTransport  );
  144. end;
  145.  
  146.  
  147. end.
  148.  

bmcsoft

  • New Member
  • *
  • Posts: 12
Re: LAMW and TThread
« Reply #16 on: December 06, 2020, 10:47:45 am »
When I create TThread in LAMW, I've got black screen on my phone. I can't see main form with jButton and jEditText.

Code: Pascal  [Select][+][-]
  1. MyThread = class(TThread)
  2. private
  3.  
  4. protected
  5.   procedure Execute; override;
  6. end;
  7.  
  8. ...
  9.  
  10. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  11. begin
  12.     sock:=TUDPBlockSocket.Create;
  13.  
  14.     ThreadBegin := MyThread.Create (false); // ThreadBegin := MyThread.Create (true) - don't work also
  15.     ThreadBegin.FreeOnTerminate := true;
  16.     ThreadBegin.Priority:=tpNormal;
  17. end;
  18.  
  19. procedure MyThread.Execute;
  20. var
  21.   buf: string;
  22. begin
  23.     sock.Bind('0.0.0.0','15001');
  24.     if sock.LastError<>0 then exit;
  25.     while True do
  26.       begin
  27.       if terminated then break;
  28.       buf := sock.RecvPacket(1000);
  29.       if sock.lasterror=0 then
  30.         begin
  31.         le2:=sock.GetRemoteSinIP;
  32.         le3:=IntToStr (sock.GetRemoteSinPort);
  33.         AndroidModule1.jEditText1.Text:=buf;
  34.         end;
  35.       sleep(1);
  36.       end;
  37. end;

When I remove "ThreadBegin := MyThread.Create (false)" string, app starts normally and main form shows jButton and jEditText.

Help me please, I'm novice in Android and LAMW. Such code in Windows works normally with TThread.

bmcsoft

  • New Member
  • *
  • Posts: 12
Re: LAMW and TThread
« Reply #17 on: December 06, 2020, 12:51:46 pm »
I found the information about it: "You don´t need any special unit for this to work with Windows. However with Linux, macOS and FreeBSD, you need the cthreads unit and it must be the first used unit of the project (the program source, usually the .lpr file)!".

Key words here is: "first used unit of the project is lpr-file". When I edit Unit1.pas with "use cthreads" it did not work!
« Last Edit: December 06, 2020, 12:53:29 pm by bmcsoft »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1742
Re: LAMW and TThread
« Reply #18 on: December 06, 2020, 09:18:57 pm »

@bmcsoft

Quote
Key words here is: "first used unit of the project is lpr-file". When I edit Unit1.pas with "use cthreads" it did not work!

So you are suggesting changing these lines of code from Unit*.pas to "controls.lpr" ??

Code: Pascal  [Select][+][-]
  1. {$IFDEF UNIX}{$IFDEF UseCThreads}
  2.   cthreads,
  3.   {$ENDIF}{$ENDIF}    
  4.  

If yes, I can do that!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

lucamar

  • Hero Member
  • *****
  • Posts: 3461
Re: LAMW and TThread
« Reply #19 on: December 07, 2020, 12:39:58 am »
So you are suggesting changing these lines of code from Unit*.pas to "controls.lpr" ??
[...snip ...]

Yes; as happens with the alternative C memory manager (cmem), if used at all those units must be the very first in the uses clause of the program itself (the "lpr", in Lazarus parlance) because they need to be initialized (and the dynamic lib loaded, if needed) for any other code to be aware of it when needed, say, in the initialization of the RTL/FCL/LCL, etc.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018