Jump to content
programmerdelphi2k

My "Execute" on Thread is not called anymore... I'm staying crazy and dont see the my error

Recommended Posts

people,

My "Execute" on Thread is not called anymore... I'm staying crazy and dont see the my error

  1. the thread is created and released with no-errors, etc..
  2. I "breakpoint it" and dont see nothing to happens... 
  3. I have the same constructions in anothers project and "Execute" is execute"D" and breakpoint is breakpoint"ED" >:)
  4. but new projects (units), does not works anymore
  5. I'm staying crazy and dont see my error on code.... help me!

 

 

 

type
  TMyThread = class(TThread)
  private

  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;

  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

 

implementation

 

{$R *.dfm}


{ TMyThread }

constructor TMyThread.Create;
begin
  inherited Create; // created (be suspended or not) OK! debug
end;

destructor TMyThread.Destroy;
begin
  inherited;  // destroyed OK! debug
end;

procedure TMyThread.Execute;
begin  
  // the "Execute" is not called anymore...   it's jumped, same in Debug mode
  inherited; // or not...
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x: TMyThread;
begin
  x := TMyThread.Create;
  try
    // x.Start; // if suspended...
  finally
    x.Free;
  end;
end;
 

Share this post


Link to post

@Der schöne Günther thanks for your quick reply

 

this thread have a long code ok but the constructions base is the same than new thread...

 

unit Unit1;

interface

uses
  System.SysUtils,
  System.Classes;

type
  TMyThread = class(TThread)
  private
    FStatusText: String;
    FThreadID  : cardinal;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean = true);
    destructor Destroy; override;
  end;

implementation

{ TMyThread }

constructor TMyThread.Create(CreateSuspended: boolean);
begin
  inherited Create(CreateSuspended);
  //
  FThreadID := ThreadID;
end;

destructor TMyThread.Destroy;
begin
  inherited;
end;

procedure TMyThread.Execute;  // is called on runtime and showed on Debug mode
begin
  // inherited;
  FStatusText := 'OnExecute: Thread in execution...' + FThreadID.ToString;
end;

end.

Share this post


Link to post

but if I create it (same) a new project ... the "Execute" is not called anymore

 

RAD STUDIO 11.2 ALEXANDRIA with Patch1 installed

Edited by programmerdelphi2k

Share this post


Link to post

other info:

  1. I have opened the old project create in RAD 11.1 Alexandia
  2. I have "duplicated" the same thread with a new name (TMyNewThread (new) = TMyThread (old) in another unit (unit2) on same project (nothing changed on config etc...)
  3. resulted:  EXECUTE IS NOT CALLED ANYMORE FOR NEW THREAD (TMyNewThread)

Sem título.png

Edited by programmerdelphi2k

Share this post


Link to post
2 hours ago, programmerdelphi2k said:

procedure TForm1.Button1Click(Sender: TObject);
var
  x: TMyThread;
begin
  x := TMyThread.Create;
  try
    // x.Start; // if suspended...
  finally
    x.Free;
  end;
end;

You can't destroy the thread object until it's finished... It doesn't even have time to start.

 

Maybe you have to do something like this:

 

var
  x: TMyThread;
begin
  x := TMyThread.Create(False);
  try
    // x.Start; // if suspended...
    WaitForSingleObject(X.Handle, INFINITE);
  finally
    x.Free;
  end;
end;

 

 

 

Edited by Davide Angeli
  • Like 3
  • Thanks 1

Share this post


Link to post
42 minutes ago, Davide Angeli said:

You can't destroy the thread object until it's finished... It doesn't even have time to start.

hi @Davide Angeli

 

very thanks for your "EYEsssssssss"  .... WOW MY GOD..... HELP MY GLASSES.....

 

look, I had tryed many open, close, open, close... IDE, project and "And I couldn't see this very basic and dominant line in the code..."

 

AAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHH, ... I'm going back to sanity now!

 

now, all is going to normality.

 

thansk again

Share this post


Link to post
5 hours ago, Davide Angeli said:

Maybe you have to do something like this:

 


var
  x: TMyThread;
begin
  x := TMyThread.Create(False);
  try
    // x.Start; // if suspended...
    WaitForSingleObject(X.Handle, INFINITE);
  finally
    x.Free;
  end;
end;

That should be using X.WaitFor() instead of WaitForSingleObject() directly.

var
  x: TMyThread;
begin
  x := TMyThread.Create(False);
  try
    // x.Start; // if suspended...
    X.WaitFor;
  finally
    x.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post

I think that too, but for now my big problem was: Have I gone crazy and no longer have the value of reality, or has the IDE rebelled and doesn't want to code anymore? 🙂

Share this post


Link to post

TThread internally checks its Terminated property before calling Execute().  When you destroy a TThread that hasn't finished yet, the TThread destructor calls Terminate() and WaitFor() on itself.  So, if you destroy a TThread before it even begins running, Execute() won't be called.  That is nothing new, TThread has behaved this way for many years.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×