Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread Exiting Problems

Posted on 1997-09-15
1
Medium Priority
?
165 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:wolfcrag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 400 total points
ID: 1344994
It would appear that your for loop in PaintIt needs to get all the way to 1000, even if the thread terminates when i = 10.

Here are 2 possible solutions:
1)  Change the for loop in PaintLoop to a while loop, and exit the loop early if the thread terminates.

2)  Create a variable or something which the thread sets to True when it's finished.  E.g. In your form's MouseClick event add the following:
    MyThread.Terminate;
    while  not OkToExit  do
        Application.ProcessMessages;
    Close;
Your thread's Execute method would then look something like this:
    OkToExit := False;
    while not Terminated do begin
        Synchronize(PaintIt);
        Delay(1000);
    end;
    OkToExit := True;

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question