Unit3.cpp
资源名称:第6章 多线程编程.rar [点击查看]
上传用户:gyjjlc
上传日期:2013-03-29
资源大小:2124k
文件大小:2k
源码类别:
多显示器编程
开发平台:
C++ Builder
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit3.h"
- #include "Unit1.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 Unit3::UpdateCaption()
- // {
- // Form1->Caption = "Updated in a thread";
- // }
- //---------------------------------------------------------------------------
- __fastcall TDisplayThread::TDisplayThread(bool CreateSuspended)
- : TThread(CreateSuspended)
- {
- FreeOnTerminate=true;
- Resume();
- }
- //---------------------------------------------------------------------------
- void __fastcall TDisplayThread::Execute()
- {
- while(!Terminated)
- {
- //等待信号事件处于信号态,一旦等到事件处于信号态随即复位至无信号态
- if(Form1->pEvent->WaitFor(10)==wrSignaled)
- //访问VCL必须用同步方法
- Synchronize(ListInMemo);
- //设置事件处于信号态
- Form1->pEvent->SetEvent();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TDisplayThread::ListInMemo(void)
- {
- //因容量有限,超过50行时,删除首行
- if(Form1->Memo1->Lines->Count>50)
- Form1->Memo1->Lines->Delete(0);
- //添加新行
- Form1->Memo1->Lines->Add(AnsiString(Form1->LetterTable,26));
- }
English
