views:

304

answers:

1

I have a problem also described here: http://www.delphigroups.info/3/9/106748.html

I have tried almost all forms of placing Application->Terminate() func everywhere in the code, following and not 'return 0', 'ExitProcess(0)', 'ExitThread(0)', exit(0). No working variant closes the app. Instead the code after Application->Terminate() statement is running.

I have two or more threads in the app. I tried calling terminate func in created after execution threads and in main thread.

Also this is not related (as far as I can imagine) with CodeGuard / madExcept (I have turned it off and on, no effect). CodeGuard turning also did not do success.

The only working code variant is to place Application->Terminate() call to any of any form button's OnClick handler. But this does not fit in my needs. I need to terminate in any place.

What I should do to terminate all the threads in C++ Builder 2010 application and then terminate the process?

+2  A: 

Application->Terminate() does not close application immediately, it only signals you want to close the application.

Terminate calls the Windows API PostQuitMessage function to perform an orderly shutdown of the application. Terminate is not immediate.

In your functions call Application->ProcessMessages() then check if the Application->Terminated property is true.

For applications using calculation-intensive loops, call ProcessMessages periodically, and also check Terminated to determine whether to abort the calculation and allow the application to terminate

For example:

void Calc()
{
  for (int x = 0; x < 1000000; ++x)
  {
    // perform complex calculation

    // check if need to exit
    Application->ProcessMessages();
    if (Application->Terminated)
    {
      break;
    } // end if
  } // end for

  // clean up
} 
stukelly
But what conditions should met? I wait for app closing for about 2 minutes and nothing happen..
ergey
I found the only way to really terminate my program in any place is to call these sequence:FormDestroy(); // also any memory free blocks i put hereTerminateProcess(GetCurrentProcess(), 0)
ergey
@ergey I have expanded my answer with an example.
stukelly
Thanks! It really fixes the thing. Now i am terminating through App->Terminate(), then in loop call ProcessMessages() and check for Terminated flag then return from child thread.But i wonder when really the RTL sets this flag? After terminating main thread or shutting down just VCL/RTL ? But if they do so i teoretically can't call any stdlib/stdio functions, because RTL is down, or there is another meaning of terminated flag.
ergey
@ergey. From the help: *ProcessMessages sets Terminated to true when it receives a WM_QUIT message.*
stukelly