zoukankan      html  css  js  c++  java
  • 线程【四】线程内定时器

    在消息队列部分,我们已经知道消息泵是以线程为单位进行分离的!

    换句话说,有N个程序,都产生了相关事件,在系统消息队列中会根据线程来区分,当线程的消息泵从系统消息队列抽取消息时,会挑属于自己线程的消息。

    当我们创建一个工程时,IDE环境已经为我们生成了主线程的代码以及消息泵,而当我们自己创建一个Worker线程时,默认是没有消息泵的,如果想要在线程内接收消息,需要添加消息泵!

    消息泵的结构:

    while  GetMessage(Msg,  0 ,  0 ,  0 )  do   begin   

       TranslateMessage(Msg);  

       DispatchMessage(Msg)  

     end ; 

    另:

    要想在线程内响应消息,没有窗口是不行的

    所以要在线程中创建窗口(AllocateHWND)(分配窗口的工作在VCL中的Timer控件中已经完成),来使之有接收消息(WM_TIMER)的能力!

    注意:在线程内创建TIMER时应该在线程函数中创建!在构造函数中创建控件的话会窗口创建至主线程中!

    (TThread.Execute本身就是对API的线程函数的封装,这个地方看一下VCL封装的线程类即可)

    unit Unit2;
    interface
    uses
      Classes,ExtCtrls,dialogs,Windows;
    type
      TMyTest = class(TThread)
      private
        FTimer:TTimer;
        procedure TimerProc(Sender: TObject);
      protected
        procedure Execute; override;
      public
        constructor create;
        destructor Destroy; override;
      end;
    implementation
    constructor TMyTest.Create;
    begin
      Inherited Create(true);
    end;
    destructor TMyTest.Destroy;
    begin
      inherited;
    end;
    procedure TMyTest.Execute;
    var
      Msg: TMsg;
    begin
       FTimer:=TTimer.Create(nil);//一定要在线程函数中创建,如果在线程的构造函数中创建,就把窗口创建到主线程中了,那线程中的TIMER肯定不会接收WM_TIMER消息。
       try
         FTimer.Enabled:=True;
         FTimer.Interval:=5000 ;
         FTimer.OnTimer:=TimerProc;
         while GetMessage(Msg, 0, 0, 0) do begin
            TranslateMessage(Msg);
            DispatchMessage(Msg)
          end;
          Terminate;
       finally
         FTimer.Free;
       end;
      { Place thread code here }
    end;
    procedure TMyTest.TimerProc(Sender: TObject);
    begin
       //业务处理
    end;
    View Code
  • 相关阅读:
    POJ1239
    HDU 2829 四边形不等式优化
    返回数字二进制的最高位位数o(n)
    矩阵快速幂 模板
    HDU4718 The LCIS on the Tree(LCT)
    HDU4010 Query on The Trees(LCT)
    HDU3487 Play With Chains(Splay)
    CF444C DZY Loves Colors
    HDU4836 The Query on the Tree(树状数组&&LCA)
    HDU4831&&4832&&4834
  • 原文地址:https://www.cnblogs.com/key-ok/p/3359005.html
Copyright © 2011-2022 走看看