Recent

Author Topic: [Solved] TMyThread.Create(True) causes error  (Read 6402 times)

teriyaki

  • New Member
  • *
  • Posts: 47
[Solved] TMyThread.Create(True) causes error
« on: January 21, 2022, 05:07:51 pm »
the code below works on Windows
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyThread = class(TThread)
  3.   private
  4.  
  5.   public
  6.  
  7.   end;


when I call

Code: Pascal  [Select][+][-]
  1. myThread := TMyThread.Create(True)

on macOS

it reports error as attached image.


Your comment welcome
« Last Edit: February 01, 2022, 08:31:28 am by teriyaki »

korba812

  • Sr. Member
  • ****
  • Posts: 390
Re: TMyThread.Create(True) causes error
« Reply #1 on: January 21, 2022, 05:16:01 pm »
The TThread.Execute method is an abstract method. You have to implement this method in your class.

Or add "cthreads" unit to "uses" section.
« Last Edit: January 21, 2022, 05:22:00 pm by korba812 »

teriyaki

  • New Member
  • *
  • Posts: 47
Re: TMyThread.Create(True) causes error
« Reply #2 on: January 22, 2022, 02:30:47 am »
I did

Code: Pascal  [Select][+][-]
  1. constructor TMyThread.Create(CreateSuspended: boolean);
  2. begin
  3.   inherited Create(CreateSuspended);
  4.   FreeOnTerminate := True;
  5. end;
  6.  
  7. procedure TMyThread.Execute;
  8. begin
  9. end;

it works well on Windows 10.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: TMyThread.Create(True) causes error
« Reply #3 on: January 22, 2022, 04:07:23 am »
As korba812 told you, you need to add the "cthreads" unit to "uses" clause.

For more detail see the Wiki article Units needed for a multi-threaded application.

teriyaki

  • New Member
  • *
  • Posts: 47
Re: TMyThread.Create(True) causes error
« Reply #4 on: January 22, 2022, 01:18:38 pm »
if I add the code below, Lazarus can build the project and create the execute app.

Code: Pascal  [Select][+][-]
  1. uses
  2. {$ifdef DARWIN}
  3.    cthreads,
  4. {$endif}
             

but the app can not run, nothing happens

if I remove the code above, it can launch the app correctly.



« Last Edit: January 22, 2022, 01:28:36 pm by teriyaki »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: TMyThread.Create(True) causes error
« Reply #5 on: January 22, 2022, 11:22:42 pm »
if I add the code below, Lazarus can build the project and create the execute app.

Code: Pascal  [Select][+][-]
  1. uses
  2. {$ifdef DARWIN}
  3.    cthreads,
  4. {$endif}
             
In which file did you add the code?

Quote
but the app can not run, nothing happens

What does the Applications > Utilities > Console log show?

What happens if you run the executable and not the application bundle?

teriyaki

  • New Member
  • *
  • Posts: 47
Re: TMyThread.Create(True) causes error
« Reply #6 on: January 27, 2022, 06:58:33 am »
I created a empty project and made it work with cthread,
but it still report error

Code: Pascal  [Select][+][-]
  1. unit mainformunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8. {$ifdef DARWIN}
  9.  cthreads,
  10. {$endif}
  11.  
  12.     IdSSLOpenSSL, Classes, SysUtils, Forms,
  13.     Controls, Graphics, Dialogs, StdCtrls, IdHTTP, ZConnection;
  14.  
  15. type
  16.   TMyThread = class(TThread)
  17.   private
  18.  
  19.     procedure completedAction;
  20.   protected
  21.     procedure Execute; override;
  22.   public
  23.     threadHTTP: TIdHTTP;
  24.  
  25.  
  26.     constructor Create(CreateSuspended: boolean);
  27.  
  28.   end;
  29.  
  30. type
  31.  
  32.   { TForm1 }
  33.  
  34.   TForm1 = class(TForm)
  35.     Button2: TButton;
  36.     IdHTTP1: TIdHTTP;
  37.     ZConnection1: TZConnection;
  38.     procedure Button1Click(Sender: TObject);
  39.     procedure Button2Click(Sender: TObject);
  40.   private
  41.  
  42.   public
  43.  
  44.   end;
  45.  
  46. var
  47.   Form1: TForm1;
  48.  
  49. implementation
  50.  
  51. {$R *.lfm}
  52.  
  53. { TForm1 }
  54.  
  55. procedure TForm1.Button1Click(Sender: TObject);
  56. var
  57.   myThread: TMyThread;
  58. begin
  59.  
  60.   myThread := TMyThread.Create(True);
  61.   myThread.threadHTTP:=TIdHTTP.Create(nil);
  62.  
  63.   myThread.Start;
  64.  
  65. end;
  66.  
  67. procedure TForm1.Button2Click(Sender: TObject);
  68. var
  69.   myThread: TMyThread;
  70. begin
  71.  
  72.   myThread := TMyThread.Create(True);
  73.   myThread.threadHTTP:=TIdHTTP.Create(nil);
  74.  
  75.   myThread.Start;
  76.  
  77. end;
  78.  
  79. constructor TMyThread.Create(CreateSuspended: boolean);
  80. begin
  81.   inherited Create(CreateSuspended);
  82.   FreeOnTerminate := True;
  83. end;
  84.  
  85. procedure TMyThread.completedAction;
  86. // this method is executed by the mainthread and can therefore access all GUI elements.
  87. begin
  88.  
  89. end;
  90.  
  91. procedure TMyThread.Execute;
  92. var
  93.   n, i: integer;
  94.   responseContentString: string;
  95.   lHTTP: TIdHTTP;
  96.   tIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
  97.  
  98. begin
  99.  
  100.   tIdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  101.   tIdSSLIOHandlerSocket.PassThrough := True;
  102.   with tIdSSLIOHandlerSocket do
  103.   begin
  104.     SSLOptions.Method := sslvTLSv1_2;
  105.     SSLOptions.SSLVersions := [sslvTLSv1_2];
  106.     SSLOptions.VerifyMode := [];
  107.     SSLOptions.VerifyDepth := 2;
  108.   end;
  109.  
  110.   responseContentString := threadHTTP.Get('https://echo.hoppscotch.io') ;//error happened here
  111.  
  112.  
  113.   Synchronize(@completedAction);  //Synchronize(@Showstatus);
  114.  
  115. end;
  116.  
  117. end.
     


cdbc

  • Hero Member
  • *****
  • Posts: 995
    • http://www.cdbc.dk
Re: TMyThread.Create(True) causes error
« Reply #7 on: January 27, 2022, 07:29:43 am »
Hi
Code: Pascal  [Select][+][-]
  1. {$ifdef DARWIN}
  2.  cthreads,
  3. {$endif}

needs to be the first in your project-file !!!!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: TMyThread.Create(True) causes error
« Reply #8 on: January 27, 2022, 12:04:24 pm »
needs to be the first in your project-file !!!!

As noted in the Wiki link I posted.

@teriyaki: please read Wiki links when provided to you as they nearly always contain useful information :)

teriyaki

  • New Member
  • *
  • Posts: 47
Re: TMyThread.Create(True) causes error
« Reply #9 on: January 27, 2022, 02:53:08 pm »
my project

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6. {$ifdef DARWIN}
  7. cthreads,
  8. {$endif}
  9.  
  10.   Interfaces, // this includes the LCL widgetset
  11.   Forms, zcomponent, indylaz, mainformunit
  12.   { you can add units after this };
  13.  
  14. {$R *.res}
  15.  
  16. begin
  17.   RequireDerivedFormResource:=True;
  18.   Application.Scaled:=True;
  19.   Application.Initialize;
  20.   Application.CreateForm(TForm1, Form1);
  21.   Application.Run;
  22. end.
  23.                  
   

mainformunit.pas

Code: Pascal  [Select][+][-]
  1. unit mainformunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  
  9.  
  10.     IdSSLOpenSSL, Classes, SysUtils, Forms,
  11.     Controls, Graphics, Dialogs, StdCtrls, IdHTTP, ZConnection;
  12.  
  13. type
  14.   TMyThread = class(TThread)
  15.   private
  16.  
  17.     procedure completedAction;
  18.   protected
  19.     procedure Execute; override;
  20.   public
  21.     threadHTTP: TIdHTTP;
  22.  
  23.  
  24.     constructor Create(CreateSuspended: boolean);
  25.  
  26.   end;
  27.  
  28. type
  29.  
  30.   { TForm1 }
  31.  
  32.   TForm1 = class(TForm)
  33.     Button2: TButton;
  34.     IdHTTP1: TIdHTTP;
  35.     ZConnection1: TZConnection;
  36.     procedure Button1Click(Sender: TObject);
  37.     procedure Button2Click(Sender: TObject);
  38.   private
  39.  
  40.   public
  41.  
  42.   end;
  43.  
  44. var
  45.   Form1: TForm1;
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.Button1Click(Sender: TObject);
  54. var
  55.   myThread: TMyThread;
  56. begin
  57.  
  58.   myThread := TMyThread.Create(True);
  59.   myThread.threadHTTP:=TIdHTTP.Create(nil);
  60.  
  61.   myThread.Start;
  62.  
  63. end;
  64.  
  65. procedure TForm1.Button2Click(Sender: TObject);
  66. var
  67.   myThread: TMyThread;
  68. begin
  69.  
  70.   myThread := TMyThread.Create(True);
  71.   myThread.threadHTTP:=TIdHTTP.Create(nil);
  72.  
  73.   myThread.Start;
  74.  
  75. end;
  76.  
  77. constructor TMyThread.Create(CreateSuspended: boolean);
  78. begin
  79.   inherited Create(CreateSuspended);
  80.   FreeOnTerminate := True;
  81. end;
  82.  
  83. procedure TMyThread.completedAction;
  84. // this method is executed by the mainthread and can therefore access all GUI elements.
  85. begin
  86.  
  87. end;
  88.  
  89. procedure TMyThread.Execute;
  90. var
  91.   n, i: integer;
  92.   responseContentString: string;
  93.   lHTTP: TIdHTTP;
  94.   tIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
  95.  
  96. begin
  97.  
  98.   tIdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  99.   tIdSSLIOHandlerSocket.PassThrough := True;
  100.   with tIdSSLIOHandlerSocket do
  101.   begin
  102.     SSLOptions.Method := sslvTLSv1_2;
  103.     SSLOptions.SSLVersions := [sslvTLSv1_2];
  104.     SSLOptions.VerifyMode := [];
  105.     SSLOptions.VerifyDepth := 2;
  106.   end;
  107.  
  108.   responseContentString := threadHTTP.Get('https://echo.hoppscotch.io') ;
  109.  
  110.  
  111.   Synchronize(@completedAction);  //Synchronize(@Showstatus);
  112.  
  113. end;
  114.  
  115. end.  

still reports error as the attached images

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TMyThread.Create(True) causes error
« Reply #10 on: January 27, 2022, 06:36:21 pm »
@teriyaki,

Did you notice the error message in your first post Is different from the message in your last post?

AND

The thread method was empty in your first post.

Meaning your problem mentioned in the first post is solved. Now you have a different problem.

Probably should create a new post for it.
Your current problem is related to OpenSSL and
You need to mention details of your setup.
« Last Edit: January 27, 2022, 06:39:52 pm by engkin »

teriyaki

  • New Member
  • *
  • Posts: 47
Re: TMyThread.Create(True) causes error
« Reply #11 on: January 28, 2022, 12:57:08 am »
in the first post, I hope to make thing simple so I did not show all codes.
At that time, if I add

Code: Pascal  [Select][+][-]
  1. uses cthread

the project could be compiled but could not run correctly.
so I created another empty project and added the code blocks one by one to keep it as a simplest project.(the code show above)
Finally it can be run now.
but it still reports same error since then

my macOS version is Monterey 12.1
« Last Edit: January 28, 2022, 12:59:12 am by teriyaki »

zoltanleo

  • Sr. Member
  • ****
  • Posts: 486
Re: TMyThread.Create(True) causes error
« Reply #12 on: January 28, 2022, 08:29:18 am »
Hey teriyaki

Try to run this project (ignore the "dirty" code :) )
« Last Edit: January 28, 2022, 08:32:15 am by zoltanleo »
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

 

TinyPortal © 2005-2018