Recent

Author Topic: LAMW and TThread  (Read 1177 times)

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
LAMW and TThread
« on: June 14, 2019, 04:27:36 am »
The below code crashes my app, id dies when it calls the line "inherited Create(TRUE)", any Ideas?

Code: Pascal  [Select]
  1. type
  2.   TTestThread = class(TThread)
  3.      procedure Execute;  override;
  4.      constructor Create;
  5.    end;
  6.  
  7.  constructor TTestThread.Create;
  8. begin
  9.   inherited Create(TRUE);
  10.   FreeOnTerminate := TRUE;
  11. end;
  12.  
  13. procedure TTestThread.Execute;
  14. begin
  15.  
  16. end;
  17.  
  18. procedure TAndroidModule2.TestThreading;
  19. begin
  20.   TTestThread.Create;
  21. end;
  22.  

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: LAMW and TThread
« Reply #1 on: June 14, 2019, 08:07:30 am »
Do you call the Create of the instance, instead of the type?

PascalDragon

  • Hero Member
  • *****
  • Posts: 674
  • Compiler Developer
Re: LAMW and TThread
« Reply #2 on: June 14, 2019, 09:06:14 am »
Does your main project file have the cthreads unit in its usage clause? This unit is required on *nix systems which Android is one of. The Lazarus templates usually have the unit protected by a {$ifdef unix}{$ifdef usecthreads}cthreads{$endif}{$endif} or so, so you need to define the usecthreads define in that case (or remove the ifdef altogether).

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #3 on: June 16, 2019, 01:50:54 am »
Thank you PascalDragon, "cThreads" did stop the app crash.

However now I can't get the OnTerminate event to be called see below code.

When run the jProgressDialog does not close.

Any Ideas?

Code: Pascal  [Select]
  1. type
  2.   TTestThread = class(TThread)
  3.      procedure Execute;  override;
  4.      constructor Create( pOnTerminateEvent : TNotifyEvent);
  5.    end;
  6.    
  7.   TAndroidModule1 = class(jForm)  
  8.      dlgTestProgress: jDialogProgress;  
  9.      thrdSendOffLine : TTestThread;      
  10.   end;  
  11.    
  12.  
  13. procedure TAndroidModule1.TestTerminated(Sender: TObject);
  14. begin
  15.   dlgTestProgress.Close;
  16. end;
  17.  
  18. constructor TTestThread.Create( pOnTerminateEvent : TNotifyEvent);
  19. begin
  20.  inherited Create(TRUE);
  21.  OnTerminate := pOnTerminateEvent;
  22.  FreeOnTerminate := TRUE;
  23. end;
  24.  
  25.  
  26. procedure TTestThread.Execute;
  27. begin
  28.  
  29. end;
  30.  
  31. procedure TAndroidModule1.RunThreadTest;
  32. begin
  33.   dlgTestProgress.Show();
  34.   thrdSendOffLine  := TTestThread.Create(TestTerminated);
  35.   thrdSendOffLine.Start;
  36. end;  
  37.  
  38.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 674
  • Compiler Developer
Re: LAMW and TThread
« Reply #4 on: June 22, 2019, 05:53:14 pm »
It could be that the main thread is not calling CheckSynchronize as OnTerminate is called using Synchronize. I don't have enough experience with Android modules in FPC, so I don't know whether there is supposed to be component that's indeed calling that function or not.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: LAMW and TThread
« Reply #5 on: June 22, 2019, 07:24:21 pm »
IIRC, your code in LAMW resides in a library (.so). Probably the best option for you to get TThread to work as you expect is to fake a main thread like in this case.

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #6 on: June 24, 2019, 05:38:47 am »
I have looked at https://forum.lazarus.freepascal.org/index.php/topic,44522.0.html.

This looks rather not usable inside the LAMW framework, unless I am mistaken.
LAMW creates a libcontrols.so library, then does a lot of packaging to make an Androd App.

I will keep looking though


jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
Re: LAMW and TThread
« Reply #7 on: June 24, 2019, 06:13:09 am »

Quote
This looks rather not usable inside the LAMW framework, unless I am mistaken....

I think you could give it a try ... LAMW lib/.so  is a "TCustomApplication",  too...

What is going to be precessed in the TThread?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #8 on: June 24, 2019, 06:49:13 am »
The application simply makes web service calls in response to the users interactions, some of these web service calls may take some time. I want a thread to process the web service call. allowing the main app to show a progress dialog until the web service API returns. This is proving far more difficult than I hoped for. I still have not worked out how the packaging of the app works, so I don't know how to add a separate library beside as this solution states can be done.

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #9 on: June 24, 2019, 06:54:32 am »
I am wondering if there could not be a "jThread" component to make threads inside LAMW applications?

jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
Re: LAMW and TThread
« Reply #10 on: June 24, 2019, 06:58:06 am »
Quote
so I don't know how to add a separate library beside as this solution states can be done....

Put your custom ".so"  [can be a simple raw ".so" ] in the same folder as  "libcontrols.so" .... 
LAMW will takes care of your library automatically...


Quote
I am wondering if there could not be a "jThread" component to make threads inside LAMW applications?

jHttpClient component has some "async" methods.....  what you need?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #11 on: June 24, 2019, 07:13:12 am »
will give AppAsyncTaskDemo1 a try it does look promising

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #12 on: June 24, 2019, 07:56:08 am »
If I could make a "WST" ( web services toolkit ) transport based on jHttpClient async methods , then we just might have a viable solution.

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #13 on: June 24, 2019, 08:59:49 am »
We have a winner "jAsyncTask"can handle the requirement

jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
Re: LAMW and TThread
« Reply #14 on: June 24, 2019, 07:40:48 pm »
Quote
We have a winner "jAsyncTask"can handle the requirement

Nice!

What about a simple demo  jAsyncTask +  WST?

You can help all LAMW/Lazarus/FreePascal  community!!!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard