Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, your code is not good (though it probably will work in 99.99% or even 100% cases). If you are planning to terminate work thread from main thread, don't set FreeOnTerminate to True (I don't see what are you trying to gain in the above code by setting FreeOnTerminate to True, it at least makes your code less understandable).</p> <p>A more important situation with terminating work threads is that you are trying to close an application while work thread is in wait state. The thread will not be awaken if you just call Terminate, generally you should use additional syncronization object (usually event) to wake up the work thread.</p> <p>And one more remark - there is no need for</p> <pre><code> begin MyThread.Terminate; MyThread.WaitFor; MyThread.Free; end; </code></pre> <p>if you look at TThread.Destroy code, it calls Terminate and WaitFor, so</p> <pre><code> MyThread.Free; </code></pre> <p>is enough (at least in Delphi 2009, have no Delphi 7 sources at hand to check).</p> <hr> <p><strong>Updated</strong></p> <p>Read mghie answer. Consider the following situation (better on 1 CPU system):</p> <p>main thread is executing</p> <pre><code>procedure TMainForm.Close; begin if not MyThreadReady then begin MyThread.Terminate; MyThread.WaitFor; MyThread.Free; end; end; </code></pre> <p>it checked MyThreadReady value (it is False) and was switched off by scheduler.</p> <p>Now scheduler switches to work thread; it executes</p> <pre><code> Synchronize(ThreadFinished); </code></pre> <p>and forces scheduler to switch back to main thread. Main thread continues execution:</p> <pre><code> MyThread.Terminate; // no problem MyThread.WaitFor; // ??? MyThread.Free; </code></pre> <p>can you say what will happen at WaitFor? I can't (requires a deeper look into TThread sources to answer, but at first glance looks like a deadlock). </p> <p>Your real error is something different - you have written an unreliable code and trying to find out is it correct or not. That is bad practice with threads - you should learn to write a reliable code instead. </p> <p>As for resources - when the TThread (with FreeOnTerminate = False) is terminated the only resources that remains allocated is Windows thread handle (it does not use substantial Windows resources after thread is terminated) and Delphi TThread object in memory. Not a big cost to be on the safe side.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload