Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.

Welcome, Guest
Guest Settings

Thread: Terminate() TThread behavior

This question is answered. Helpful answers available: 2. Correct answers available: 1.

Permlink Replies: 0
Dean Clark

Posts: 50
Registered: 6/12/08
Terminate() TThread behavior  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2014 12:21 PM
Under what conditions does calling Terminate() on a thread not cause the thread to terminate?

Our situation is: We have a thread class that does some processing, typically taking several minutes per run. In a form, we create a set of threads and save them in a list. The thread has a function to call if it finishes successfully, which is separate from the OnTerminate callback, which just removes the thread pointer from the list. FreeOnTerminate is true.

The thread's Execute function is a simple loop:
void __fastcall RegisterImages::Execute()
	TThread::NameThreadForDebugging(AnsiString(ID));  // ID is a sequential int
	for (int n = 0; n < 1000 && !Terminated; ++n)
                // snip...
		size_t i = 0;
		while (i < pop.size() && !Terminated)  // pop is an array of coefficients for the processing
                        // ... snip unimportant detail
		if (Terminated)
                // snip...

All of which works great, and then we added a button to stop all the threads. We simply loop through the list, calling Terminate() on each thread. However, none of the threads stop. No OnTerminate handlers are called, the threads are still displayed in the Thread Status list, and according to Windows Task Manager something is still cranking the cores -- however, the threads never complete, nor produce any output, nor do their breakpoints work. They seem orphaned.

All the documentation implies that calling Terminate() should set Terminated to true, which should cause the loop to end, which should call the OnTerminate handler, and then the thread should free itself. None of this seems to happen. But why not?


Edited by: Dean Clark on Dec 14, 2014 3:24 PM
Never mind, the error was elsewhere. Sorry for the false alarm.
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02