INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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?

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

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

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

(OP)
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.

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

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...

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

(OP)
>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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close