Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

So-Called "Safe" Way To Terminate Threads Causes Access Violations 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
Very simple example (actually within the code):

Code:
procedure UpdateThread.Execute;
begin
  While not Terminated do
    begin
      Synchronize(Form1.UpdateStuff);
      WaitForSingleObject(Self.Handle, 50);  // this is for a "safe" thread delay, attempting to not soak the CPU.
    end;
end;

procedure TForm1.UpdateStuff;
begin
  // do stuff with Form1 that generates Access Violations.
end;

The problem is I'm trying to do this the so-called "safe" way (using TerminateThread actually makes this program work *right*), and yet the thread continues after the form is forced closed (works otherwise), generating access violations in "UpdateStuff".

Code:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ut.Terminate;
  ut.WaitFor;
  ut.Free;
end;

OnCloseQuery produces the same result. So...thoughts?

 
I just put "CanClose = False" on the OnCloseQuery and it still killed the form and AVed the program...

 
Okay, answered by a small note in one of the form documentation items. "OnClose" is not called in child forms upon the termination of a main form (i.e. program). So all this kind of stuff *must* occur in OnDestroy *as well as* OnClose.

 
It seems that your "updatethread" is not doing any real work worth of threading.
You can replace it by a TTimer?

/Daddy

-----------------------------------------------------
Helping people is my job...
 
>You can replace it by a TTimer?

Probably. Given all the other things I have going on with the main thread, I'm not sure how it would affect things. But it's working (the main problem was starting the thread when the form starts and stopping it when the form ends) now so I'm not too worried at the moment.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top