//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "WorkerThread.h"

//#pragma package(smart_init)
//---------------------------------------------------------------------------

  /*
   *Author  : ¿½¨R Xiao Chong
   *Email   : qs.xiao@gmail.com
   *Date    : 20060913
   *TODO    :
   *Compiler: BCB6
   *Warning : If you will create this thread more than one simutaneously,
              be care using global TCriticalSection object between every
              time you wanna get gloabl resource from MainForm.
  **/
//---------------------------------------------------------------------------

__fastcall Worker::Worker(bool CreateSuspended)
  : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall Worker::Execute()
{
  WORK_ITEM item;
  while (!Terminated && WaitForSingleObject(MainForm->mDoWorkFlag,INFINITE)==0L)
  {

    item = MainForm->mQue.front(); //get the item from queue
    item.mpFunction(item); //run user define function

    MainForm->mQue.pop_front();  //remove front queue item

    //VCL is not thread-safe comopnent, alter to MainForm execute
    Synchronize(UpdateMemo);

    if (MainForm->mQue.size()==0) //if no work to do
      ::ResetEvent(MainForm->mDoWorkFlag);  //will stop at while


  }

}
//---------------------------------------------------------------------------

void __fastcall Worker::UpdateMemo(void)
{
    MainForm->Memo2->Lines->Delete(0); // delete first item from memo2
}
