Recent

Author Topic: Thread issue, App closes immediately  (Read 1378 times)

dimonershov

  • Newbie
  • Posts: 5
Thread issue, App closes immediately
« on: February 22, 2023, 01:19:29 pm »
Hi all! I need help.

I use laz4android. This is my first app with a Thread. My application closes immediately! :(

Source code:

Code: Pascal  [Select][+][-]
  1. {hint: Pascal files location: ...\Potok\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes, SysUtils, AndroidWidget, Laz_And_Controls;
  13.  
  14. type
  15.  
  16.   { TAndroidModule1 }
  17.  
  18.   TAndroidModule1 = class(jForm)
  19.     Button1: jButton;
  20.     TextView1: jTextView;
  21.     procedure Button1Click(Sender: TObject);
  22.   private
  23.     {private declarations}
  24.   public
  25.     {public declarations}
  26.   end;
  27.  
  28.  
  29. type
  30.  
  31.   { ShdkThread }
  32.  
  33.  ShdkThread=class(TThread)
  34.   private
  35.    stroka: string;
  36.    procedure Show;
  37.   protected
  38.    procedure Execute; override;
  39.  
  40. end;
  41.  
  42. var
  43.   AndroidModule1: TAndroidModule1;
  44.   potok: ShdkThread;
  45.  
  46. implementation
  47.  
  48. {$R *.lfm}
  49.  
  50. { ShdkThread }
  51.  
  52. procedure ShdkThread.Show;
  53. begin
  54.  stroka:='Rabotaet';
  55.  AndroidModule1.TextView1.Text:=stroka;
  56. end;
  57.  
  58. procedure ShdkThread.Execute;
  59. begin
  60.  Synchronize(Show);
  61. end;
  62.  
  63. { TAndroidModule1 }
  64.  
  65. procedure TAndroidModule1.Button1Click(Sender: TObject);
  66. begin
  67.   potok:=ShdkThread.Create(true);
  68.   potok.FreeOnTerminate:=true;
  69.   potok.stroka:='123';
  70.   potok.Priority:=tpLower;
  71.   potok.Resume;
  72. end;
  73. end.
  74.  

Why does the app close when the button is clicked?

         
« Last Edit: February 22, 2023, 01:34:13 pm by dimonershov »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Thread issue, App closes immediately
« Reply #1 on: February 22, 2023, 01:27:00 pm »
Welcome to forum!
Please use the [ # ] button and put your code within the [ code ] my code [ / code ] tag. Thank you.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: Thread issue, App closes immediately
« Reply #2 on: February 22, 2023, 02:13:43 pm »
Did you define UseCThreads somewhere?

Usually it looks like
Code: Pascal  [Select][+][-]
  1. {$define UseCThreads}
  2.  
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7. ...

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

But AFAIK this must be in .lpr, at least for "normal" unixes.
« Last Edit: February 22, 2023, 02:24:20 pm by tetrastes »

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Thread issue, App closes immediately
« Reply #3 on: February 22, 2023, 05:20:30 pm »
An uneducated guess:

Should it not read: Synchronize(@Show);

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Thread issue, App closes immediately
« Reply #4 on: February 22, 2023, 05:39:09 pm »
That is a good guess, but there is more because this:
Code: Pascal  [Select][+][-]
  1. procedure ShdkThread.Execute;
  2. begin
  3.  Synchronize(Show);
  4. end;
means he is misusing a thread and actually executes something what looks like a thread without using the benefits of a thread: It simply executes in consecutve order. (a.k.a. a Threat!  >:D )
Bad code.
The output/result of a thread can be synchronized if needed, but plz not the whole thread....That is completely missing the point.

I know thread programming is difficult, but that is because it is, well, difficult.
« Last Edit: February 22, 2023, 05:48:08 pm by Thaddy »
Specialize a type, not a var.

dimonershov

  • Newbie
  • Posts: 5
Re: Thread issue, App closes immediately
« Reply #5 on: February 22, 2023, 08:39:12 pm »
Thank you!

In control.lpr it worked! Application doesn't crash

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

But  my thread doesn't work! Help me? I press the button, nothing happens. The main form has only text and a button.

Code: Pascal  [Select][+][-]
  1. {hint: Pascal files location: ...\Potok\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.  Classes, SysUtils, AndroidWidget, Laz_And_Controls;
  10.  
  11. type
  12.  
  13.   { ShdkThread }
  14.  
  15.  ShdkThread=class(TThread)
  16.   private
  17.    stroka: string;
  18.    stop: boolean;
  19.    ii: integer;
  20.    procedure Show;
  21.   protected
  22.    procedure Execute; override;
  23.   public
  24.     constructor Create(CreateSuspended: boolean);
  25. end;
  26.  
  27.  
  28. type
  29.  
  30.   { TAndroidModule1 }
  31.  
  32.   TAndroidModule1 = class(jForm)
  33.     Button1: jButton;
  34.     TextView1: jTextView;
  35.     procedure Button1Click(Sender: TObject);
  36.   private
  37.     {private declarations}
  38.   public
  39.     {public declarations}
  40.   end;
  41.  
  42.  
  43. var
  44.   AndroidModule1: TAndroidModule1;
  45.   potok: ShdkThread;
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TAndroidModule1 }
  52.  
  53. procedure TAndroidModule1.Button1Click(Sender: TObject);
  54. begin
  55.  potok:=ShdkThread.Create(true);
  56.  potok.FreeOnTerminate:=true;
  57.  potok.Priority:=tpNormal;
  58.  potok.Resume;
  59.  //potok.Start;
  60. end;
  61.  
  62.  
  63. { ShdkThread }
  64.  
  65. procedure ShdkThread.Show;
  66. begin
  67.  AndroidModule1.TextView1.Text:=stroka;
  68. end;
  69.  
  70. procedure ShdkThread.Execute;
  71. var
  72.   newStatus : string;
  73. begin
  74.  stroka:='TMyThread Starting ...';
  75.  Synchronize(Show);
  76.  stroka:='TMyThread Running ...';
  77.  while (not Terminated) and (true {any condition required}) do begin
  78.  
  79.     //here goes the code of the main thread loop
  80.     newStatus:='TMyThread Time: '+FormatDateTime('YYYY-MM-DD HH:NN:SS',Now);
  81.  
  82.     if NewStatus <> stroka then begin
  83.       stroka:=newStatus;
  84.       Synchronize(Show);
  85.     end;
  86.     sleep(50); // alternatively the thread may wait for an event. E.g., external I/O
  87.   end;
  88.  
  89. end;
  90.  
  91. constructor ShdkThread.Create(CreateSuspended: boolean);
  92. begin
  93.   FreeOnTerminate:=True;
  94.   inherited Create(CreateSuspended);
  95. end;
  96. end.
  97.  



tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: Thread issue, App closes immediately
« Reply #6 on: February 22, 2023, 08:50:49 pm »
An uneducated guess:

Should it not read: Synchronize(@Show);

Not in {$mode delphi}

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: Thread issue, App closes immediately
« Reply #7 on: February 22, 2023, 09:26:24 pm »
But  my thread doesn't work! Help me? I press the button, nothing happens. The main form has only text and a button.

I cannot say why it doesn't work at android. At windows it works. But change your code
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. begin
  3.  if not Assigned(potok) then
  4.  begin
  5.    potok:=ShdkThread.Create(true);
  6.    potok.FreeOnTerminate:=true;
  7.    potok.Priority:=tpNormal;
  8.    potok.Resume;
  9.    //potok.Start;
  10.  end;
  11. end;

and add smth like this
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.  if Assigned(potok) then
  4.  begin
  5.     potok.Terminate;
  6.     potok.WaitFor;
  7.  end;
  8. end;

to avoid access violations.
« Last Edit: February 22, 2023, 09:37:11 pm by tetrastes »

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: Thread issue, App closes immediately
« Reply #8 on: February 23, 2023, 04:41:16 am »
But  my thread doesn't work! Help me? I press the button, nothing happens. The main form has only text and a button.

Code: Pascal  [Select][+][-]
  1. ...
  2. Synchronize(Show);
  3. ...

The last time I tried (in 2018), TThread.Synchronize did not work correctly using LAMW.  To work around the issue I used a timer to periodically check the status of thread operations.  Reference these discussions:

https://forum.lazarus.freepascal.org/index.php/topic,41523
https://forum.lazarus.freepascal.org/index.php/topic,57311


dimonershov

  • Newbie
  • Posts: 5
Re: Thread issue, App closes immediately
« Reply #9 on: February 23, 2023, 07:27:32 pm »
Thanks everyone! I'll upload the source code tomorrow.

dimonershov

  • Newbie
  • Posts: 5
Re: Thread issue, App closes immediately
« Reply #10 on: February 24, 2023, 09:01:55 pm »
Worked app is attached. For my small task i used jtimer and CheckSynchronize() function. Very nice! I will write more.


 

TinyPortal © 2005-2018