Nil variable when thread comeplete

Hi there,

I have a thread for example TMyThread, and then i have this

var
  MyThread : TMyThread;

as global.

Then I have Button1 -> Onclick ->
 begin
   If MyThread = nil then
   begin
     MyThread := TMyThread.create;
     MyThread.Resume;
    end;
  end;

And my thread goes on it's happy way. Now after 5 minutes when I click on the button again, nothing happens. It seems when the Thread is finished it dosn't set the varialbe back to nil.

How can I set MyThread variable to nil once the thread is complete? (freeonterminate is set to true).

LVL 10
wildzeroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:
Create a TNotify event (eg, on the form)

type
  TForm1         =  class(TForm)
     procedure   FormCreate(Sender: TObject);
  private
     // Private declarations
  protected
     // Protected declarations
     procedure   OnTerminate(Sender: TObject); // <- To handle the thread OnTerminate
  public
     // Public declarations
  end;


---
then implement it as:

procedure TForm1.OnTerminate(Sender: TObject);
begin

  MyThread:=nil;

end;

----

then bind the thread's OnTerminate event to this:

   If MyThread = nil then
   begin
     MyThread := TMyThread.create;
     MyThread.OnTerminate:=Form1.OnTerminate;
     MyThread.Resume;
    end;

---

Regards,
Russell

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maradamCommented:
Freeing does not set automaticallly all the referencing variables to nil. If you are sure that the thread is terminated just set MyThread variable to nil. Buf finding out is not so easy in your case. If the thread is terminated (and then freed) you can't check Terminated property. Maybe a good idea is not to set FreeOnTerminate to true add the code below before checking MyThread = nill

if MyThread.Terminated begin
  MyThread.Free;
  MyThread:=nil;
end
if MyThread = nil ...
0
wildzeroAuthor Commented:
Hi Russell, that looks perfect! I should of thought of that.

Maradam - Not quite what I was after there.

Btw - make sure to check out my other thread question I just opened ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.