Thread issue, App closes immediately lazarus / 43-Android ---------------------------------------------------------------------- dimonershov · 2023-02-22 12:19 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {hint: Pascal files location: ...\Potok\jni }unit unit1; {$mode delphi} interface uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, SysUtils, AndroidWidget, Laz_And_Controls;  type   { TAndroidModule1 }   TAndroidModule1 = class(jForm)    Button1: jButton;    TextView1: jTextView;    procedure Button1Click(Sender: TObject);  private    {private declarations}  public    {public declarations}  end;  type   { ShdkThread }  ShdkThread=class(TThread)  private   stroka: string;   procedure Show;  protected   procedure Execute; override; end; var  AndroidModule1: TAndroidModule1;  potok: ShdkThread; implementation  {$R *.lfm} { ShdkThread } procedure ShdkThread.Show;begin stroka:='Rab otaet'; AndroidModule1.TextView1.Text:=stroka;end; procedure ShdkThread.Execute;begin Synchronize(Show);end; { TAndroidModule1 } procedure TAndroidModule1.Button1Click(Sender: TObject);begin  potok:=ShdkThread.Create(true);  potok.FreeOnTerminate:=true;  potok.stroka:='123';  potok.Priority:=tpLower;  potok.Resume;end;end.  Why does the app close when the button is clicked? ---------------------------------------------------------------------- KodeZwerg · 2023-02-22 12:27 Welcome to forum! Please use the [ # ] button and put your code within the [ code ] my code [ / code ] tag. Thank you. ---------------------------------------------------------------------- tetrastes · 2023-02-22 13:13 Did you define UseCThreads somewhere? Usually it looks like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$define UseCThreads} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}... or simply Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}... But AFAIK this must be in .lpr, at least for "normal" unixes. ---------------------------------------------------------------------- Peter H · 2023-02-22 16:20 An uneducated guess: Should it not read: Synchronize(@Show); ---------------------------------------------------------------------- Thaddy · 2023-02-22 16:39 That is a good guess, but there is more because this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ShdkThread.Execute;begin Synchronize(Show);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!  ) 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. ---------------------------------------------------------------------- dimonershov · 2023-02-22 19:39 Thank you! In control.lpr it worked! Application doesn't crash Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}... 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {hint: Pascal files location: ...\Potok\jni }unit unit1; {$mode delphi} interface uses Classes, SysUtils, AndroidWidget, Laz_And_Controls; type   { ShdkThread }  ShdkThread=class(TThread)  private   stroka: string;   stop: boolean;   ii: integer;   procedure Show;  protected   procedure Execute; override;  public    constructor Create(CreateSuspended: boolean);end;  type   { TAndroidModule1 }   TAndroidModule1 = class(jForm)    Button1: jButton;    TextView1: jTextView;    procedure Button1Click(Sender: TObject);  private    {private declarations}  public    {public declarations}  end;  var  AndroidModule1: TAndroidModule1;  potok: ShdkThread; implementation  {$R *.lfm} { TAndroidModule1 } procedure TAndroidModule1.Button1Click(Sender: TObject);begin potok:=ShdkThread. Create(true); potok.FreeOnTerminate:=true; potok.Priority:=tpNormal; p otok.Resume; //potok.Start;end;  { ShdkThread } procedure ShdkThread.S how;begin AndroidModule1.TextView1.Text:=stroka;end; procedure ShdkThread.Execute;var  newStatus : string;begin stroka:='TMyThread Starting ...'; Synchronize(Show); stroka:='TMyThread Running ...'; while (not Terminated) and (true {any condition required}) do begin     //here goes the code of the main thread loop    newStatus:='TMyThread Time: '+FormatDateTime('YYYY-MM-DD HH:NN:SS',Now);     if NewStatus <> stroka then begin      stroka:=newStatus;      Synchronize(Show);    end;    sleep(50); // alternatively the thread may wait for an event. E.g., external I/O  end; end; constructor ShdkThread.Create(CreateSuspended: boolean);begin  FreeOnTerminate:=True;  inherited Create(CreateSuspended);end;end. ---------------------------------------------------------------------- tetrastes · 2023-02-22 19:50 Quote from: Peter H on February 22, 2023, 05:20:30 pmAn uneducated guess: Should it not read: Synchronize(@Show); Not in {$mode delphi} ---------------------------------------------------------------------- tetrastes · 2023-02-22 20:26 Quote from: dimonershov on February 22, 2023, 08:39:12 pmBut  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.Button1Click(Sender: TObject);begin if not Assigned(potok) then begin   potok:=ShdkThread.Create(true);   potok.FreeOnTerminate:=true;   potok.Priority:=tpNormal;   potok.Resume;   //potok.Start; end;end; and add smth like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin if Assigned(potok) then begin    potok.Terminate;    potok.WaitFor; end;end; to avoid access violations. ---------------------------------------------------------------------- WayneSherman · 2023-02-23 03:41 Quote from: dimonershov on February 22, 2023, 08:39:12 pmBut  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...Synchronize(Show);... 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 · 2023-02-23 18:27 Thanks everyone! I'll upload the source code tomorrow. ---------------------------------------------------------------------- dimonershov · 2023-02-24 20:01 Worked app is attached. For my small task i used jtimer and CheckSynchronize() function. Very nice! I will write more.