Unit2.cpp
上传用户:gyjjlc
上传日期:2013-03-29
资源大小:2124k
文件大小:2k
源码类别:

多显示器编程

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. //文件:Unit2.cpp
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include "Unit2.h"
  6. #include "Unit1.h"
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. //   Important: Methods and properties of objects in VCL can only be
  10. //   used in a method called using Synchronize, for example:
  11. //
  12. //      Synchronize(UpdateCaption);
  13. //
  14. //   where UpdateCaption could look like:
  15. //
  16. //      void __fastcall Unit2::UpdateCaption()
  17. //      {
  18. //        Form1->Caption = "Updated in a thread";
  19. //      }
  20. //---------------------------------------------------------------------------
  21. __fastcall TsimpleThread::TsimpleThread(bool CreateSuspended)
  22.     : TThread(CreateSuspended)
  23. {
  24.     mCount=0;
  25.     pLabel=NULL;
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TsimpleThread::Execute()
  29. {
  30.     //线程终止事件
  31.     OnTerminate=Form1->ThreadOnEnd;
  32.     //现程中断属性无效时持续循环
  33.     while(!Terminated)
  34.     {
  35.         //借助于现程同步方调用DoWork函数
  36.         Synchronize(DoWork);
  37.     }
  38.     //线程结束提示
  39.     MessageBox(NULL,"线程结束!","信息",MB_OK);
  40. }
  41. //---------------------------------------------------------------------------
  42. void __fastcall TsimpleThread::AssignLabel(TLabel * pl)
  43. {
  44.     pLabel=pl;
  45. }
  46. void __fastcall TsimpleThread::DoWork(void)
  47. {
  48.     //如果pLabel指向一个TLabel控件...
  49.     if(pLabel)
  50.         //为TLabel控件的Caption属性赋值
  51.         pLabel->Caption = mCount;
  52.     if(mCount<10000)
  53.         //计数未到头加一
  54.         mCount++;
  55.     else
  56.         //计数到头复位
  57.         mCount = 0;
  58. }