⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workerthread.~cpp

📁 C++ Builer 多執行緒及Que 範例程式
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "WorkerThread.h"

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

  /*
   *Author  : 拷≧ 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
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -