Link to home
Start Free TrialLog in
Avatar of wolfcrag
wolfcrag

asked on

Thread Exiting Problems

Hello,

    I have a thread which does some painting on the screen. During the Execute method I repeatedly call a procedure (which is in another unit). This procedure itself has a longish (1 or 2 seconds) for...do loop, and I need to include Application.ProcessMessages in this loop, because if the user clicks the form during this loop, I want the application to end (a la screensaver). So in the execute method of MyThread I have:

 while not Terminated do begin
      Synchronize (PaintIt);
      Delay(1000)           // 1 second
 end

PaintIt is a thread procedure which has the following code:

//some code before this
PaintLoop(Canvas);

and finally, PaintLoop, is a procedure in another unit which looks like:

for i := 1 to 1000 do begin
  BitBlt(bla bla bla);
  Application.ProcessMessages;
end;

So, in the mouse click event of the main form, I say:

MyThread.Terminate;
Close;

The PROBLEM is that after the FormClose event, the application doesn't end, and attempts to go back to the for...do loop in the PaintLoop procedure. Problem, of course, is that all of the canvases have by now been Free'd, and I therefore get an access violation. Why is it going back to the for...do loop, and what can I do to stop this!?

Note: If I click on the form after the for...do loop (i.e. during the 1 second delay) then the application terminates nicely.
ASKER CERTIFIED SOLUTION
Avatar of JimBob091197
JimBob091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial