Unit2.cpp
资源名称:第6章 多线程编程.rar [点击查看]
上传用户:gyjjlc
上传日期:2013-03-29
资源大小:2124k
文件大小:2k
源码类别:
多显示器编程
开发平台:
C++ Builder
- //---------------------------------------------------------------------------
- //文件:Unit2.cpp
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit2.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 Unit2::UpdateCaption()
- // {
- // Form1->Caption = "Updated in a thread";
- // }
- //---------------------------------------------------------------------------
- __fastcall TsimpleThread::TsimpleThread(bool CreateSuspended)
- : TThread(CreateSuspended)
- {
- mCount=0;
- pLabel=NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TsimpleThread::Execute()
- {
- OnTerminate=Form1->ThreadOnEnd;
- //现程中断属性无效时持续循环
- while(!Terminated)
- {
- //借助于现程同步方调用DoWork函数
- Synchronize(DoWork);
- }
- //线程结束提示
- MessageBox(NULL,"线程结束!","信息",MB_OK);
- }
- //---------------------------------------------------------------------------
- void __fastcall TsimpleThread::AssignLabel(TLabel * pl)
- {
- pLabel=pl;
- }
- void __fastcall TsimpleThread::DoWork(void)
- {
- //如果pLabel指向一个TLabel控件...
- if(pLabel)
- //为TLabel控件的Caption属性赋值
- pLabel->Caption = mCount;
- if(mCount<10000)
- //计数未到头加一
- mCount++;
- else
- //计数到头复位
- mCount = 0;
- }
English
