Recent

Author Topic: My TThread WON'T START  (Read 1733 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
My TThread WON'T START
« on: November 21, 2017, 08:11:19 pm »
Hi all,

I got a bit of an issue. I am running the latest fpc and lazarus. I created a very simple thread and it just won't start NO MATTER what I do. However, I ran the example multithreaded program for lazarus and that does run. I compared my thread program to the example and I can't find anything wrong. The only difference is the Execute method everything else is the same. I am working on the Linux System. So, why can't I create and run my thread? Can someone please help me? thank you.

1.) I did make sure that cthreads is included in my lpr file.
Code: Pascal  [Select][+][-]
  1. {$IFDEF UNIX}{$IFDEF UseCThreads}
  2. cthreads,
  3. {$ENDIF}
  4.  

2.) the code is as follows

Code: Pascal  [Select][+][-]
  1. uses
  2. Tmythread = class(TThread)
  3. protected
  4. procedure Execute; override;
  5. public
  6. constructor create(createsuspended:boolean);
  7. end;
  8.  
  9. var
  10. mythread:Tmythread;
  11.  
  12. procedure Tmythread.Execute;
  13. begin
  14.       //Do something
  15. end;
  16.  
  17. procedure TmyThread.Create(createsuspended:boolean);
  18. begin
  19.      FreeonTerminate:=true;
  20.      inherited Create(createsuspended);
  21. end;
  22.  
  23. procedure TForm1.Create(sender:TObject);
  24. begin
  25.      mythread := Tthread.create(true);
  26.      mythread.start;
  27. end;


« Last Edit: November 21, 2017, 08:40:59 pm by Awesome Programmer »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: My TThread WON'T START
« Reply #1 on: November 21, 2017, 09:32:49 pm »
Such a thread with an empty .execute executes, and then terminates in a few tenths or hundredths of a microsecond.

So why are you so sure it really doesn't start (rather than start and finish very quickly?)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: My TThread WON'T START
« Reply #2 on: November 21, 2017, 09:36:45 pm »
Oops... Sorry... I figured out what my problem was...

I created an instance of TThread thread not TMythread. Once I made the correction, it works... thanks. :)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: My TThread WON'T START
« Reply #3 on: November 21, 2017, 09:38:49 pm »
You have two problems besides an empty Execute.
You need
Code: Pascal  [Select][+][-]
  1. {$Define usecthreads}
in your .lpr before other compiler directives,

and you need
Code: Pascal  [Select][+][-]
  1. mythread := TmyThread.Create(True);
in your form's OnCreate handler.

 

TinyPortal © 2005-2018