//----------------------------------------------------------------------------//
//    module ThrTune.cpp                                                      //
//                                                                            //
//    Thread function: Tune parameters using TTune                            //
//                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software and can be used under the terms of the    //
//    GNU licence. See header file for further information and disclaimer.    //
//                                                                            //
//----------------------------------------------------------------------------//


#include <vcl.h>
#pragma hdrstop

#include "ThrTune.h"
#pragma package(smart_init)


#include "FMain.h"         // due to:  TFMain
#include "cluster.h"       //          TCluster

//---------------------------------------------------------------------------------------------------------------------------------------
// constructor
__fastcall ThrTune::ThrTune(bool CreateSuspended, TFMain*const& _main, TTune*const& _tuningResults) : TThread(CreateSuspended)
{
   main = _main;                       // a) copy
   tuningResults = _tuningResults;

   Priority        = tpNormal;         // b) set thread's properties
   OnTerminate     = &OnFinish;
   FreeOnTerminate = true;
}


//---------------------------------------------------------------------------------------------------------------------------------------
// thread function: execute TTune::DoIt()
void __fastcall ThrTune::Execute()
{
  try
   {
      tuningResults->DoIt(&main->GetStopFlag());
   }
   catch(TExceptionU excp)                         // exception handling 'type U' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(TExceptionAB excp)                        // exception handling 'type AB' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(...)                                      // all other exceptions
   {
      strcpy(szText, "C++ Exception ;-) (Executing)");// copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
}


//----------------------------------------------------------------------------------------------------------------------
// call when tuning is finished
void __fastcall ThrTune::OnFinish(TObject*)
{
   try
   {
      main->OnTuningFinished();
   }
   catch(TExceptionU excp)                         // exception handling 'type U' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(TExceptionAB excp)                        // exception handling 'type AB' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(...)                                      // all other exceptions
   {
      strcpy(szText, "C++ Exception ;-)");            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
}


//----------------------------------------------------------------------------------------------------------------------
// display error message
void __fastcall ThrTune::MessageBox()
{
   Application->MessageBox(szText, "Error Tuning Parameters", MB_OK | MB_ICONERROR);
}