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.     OnTerminate=Form1->ThreadOnEnd;
  31.     //现程中断属性无效时持续循环
  32.     while(!Terminated)
  33.     {
  34.         //借助于现程同步方调用DoWork函数
  35.         Synchronize(DoWork);
  36.     }
  37.     //线程结束提示
  38.     MessageBox(NULL,"线程结束!","信息",MB_OK);
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TsimpleThread::AssignLabel(TLabel * pl)
  42. {
  43.     pLabel=pl;
  44. }
  45. void __fastcall TsimpleThread::DoWork(void)
  46. {
  47.     //如果pLabel指向一个TLabel控件...
  48.     if(pLabel)
  49.         //为TLabel控件的Caption属性赋值
  50.         pLabel->Caption = mCount;
  51.     if(mCount<10000)
  52.         //计数未到头加一
  53.         mCount++;
  54.     else
  55.         //计数到头复位
  56.         mCount = 0;
  57. }