www.pudn.com > Producer&cusuom.rar > UnitThreadProducter.cpp


//--------------------------------------------------------------------------- 
 
#include  
#pragma hdrstop 
#include "UnitThreadProducter.h" 
#pragma package(smart_init) 
//--------------------------------------------------------------------------- 
 
//   Important: Methods and properties of objects in VCL can only be 
//   used in a method called using Synchronize, for example: 
// 
//      Synchronize(UpdateCaption); 
// 
//   where UpdateCaption could look like: 
// 
//      void __fastcall Unit2::UpdateCaption() 
//      { 
//        Form1->Caption = "Updated in a thread"; 
//      } 
//--------------------------------------------------------------------------- 
 
__fastcall TProducterThread::TProducterThread(bool CreateSuspended) 
        : TThread(CreateSuspended) 
{ 
  FreeOnTerminate=true; 
  Count=0; 
  TPB=NULL; 
  TB=NULL; 
  TE=NULL; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TProducterThread::Execute() 
{ 
  if(!Terminated) 
   { 
    DoWork(); 
    //Synchronize(DoWork); // 
    //TQ->Acquire(); 
    TB->Caption="停工"; 
    TB->Enabled=false; 
    Tmppt=NULL; 
    TmpProducterThread=new TStreamProduct(true); 
    Tmppt=(freeproducts *)malloc(sizeof(freeproducts)); 
    if(Tmppt==NULL) 
      { 
       Application->MessageBox("内存不足,无法分配存储空间","系统信息:内存分配错误",MB_OK); 
       //ShowMessage("内存不足,无法分配存储空间"); 
      } 
    Tmppt->TSP=TmpProducterThread; 
    if((lpHead==NULL)&&(lpEnd==NULL)) 
     { 
      lpHead=Tmppt;                   
      lpEnd=Tmppt; 
     } 
    else 
     { 
      Tmppt->PTRStread=NULL; 
      lpEnd->PTRStread=Tmppt; 
      lpEnd=Tmppt; 
     } 
   TE->Text=IntToStr(StrToInt(TE->Text)+1); 
   //TQ->Release(); 
   } 
} 
//--------------------------------------------------------------------------- 
void __fastcall TProducterThread::ProductThing(TProgressBar *proces,TButton *But,TEdit *edit)//,freeproducts *pt1,freeproducts *pt2,TCriticalSection *tq;) 
{ 
  Count=0; 
  TPB=proces; 
  TB=But; 
  TE=edit; 
} 
void __fastcall TProducterThread::DoWork() 
{ 
   for(Count=0;Count<400;Count++) 
   { 
    //TQ->Acquire(); 
    if(TPB->PositionMax) 
      { 
       TPB->StepIt(); 
      } 
    //TQ->Release(); 
   } 
  TPB->Position=0; 
}