如果程序现在正在执行a事件,timer事件激活了,需要去处理b事件。
程序是怎么运行呢?
是先处理完a事件,再去处理b事件
暂停a事件,先去处理b事件,处理完b,再回来处理a事件。
还是a、b事件同时处理?

解决方案 »

  1.   

    在a里面激活的timer吗?只要timer被激活,都是异步执行的,相当于新开一个线程定时执行timer事件中的过程
      

  2.   

    Timer的OnTimer是在主线程中执行的,如果这个时候其他事件正在执行,会等到执行完之后才响应OnTimer的,
    如 Button.OnClick执行时,是不会触发OnTimer的,除非在OnClick中执行了Application.ProcessMessages或者其他等同的执行(注意如果OnClick中有执行FormXX.ShowModule不在此范围内)
      

  3.   

    Timer 准确讲叫IdleTimer,仅在没有任何任务时才会发生。Timer事件经常会被Windows丢弃。
      

  4.   

    如kiboisme所说,要等a事件循环完成,才会执行时钟事件var
      Form1: TForm1;
      k: Integer;
      Ti: TTimer;
    implementation{$R *.dfm}procedure Tform1.tionTimer(sender: Tobject);
    begin
      Inc(k);
      Button1.Caption := IntToStr(k);
       if k > 20 then
       Ti.Enabled:=False ;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      i: integer;
    begin
      Ti := TTimer.Create(self);
      Ti.OnTimer := tionTimer;
      Ti.Interval := 1000;
      Ti.Enabled := False;
      for i := 1 to 10 do
      begin
        Sleep(1000);
        Button2.Caption := IntToStr(i);
        if i > 5 then
          Ti.Enabled := True;
      end;
    end;
      

  5.   


    var
      k: Integer;
      Ti: TTimer;procedure Tform1.tionTimer(sender: Tobject);
    begin
      Inc(k);
      Button1.Caption := IntToStr(k);
      if k > 20 then
        Ti.Enabled := False;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      i: integer;
    begin
      if not Assigned(ti) then
      begin
        Ti := TTimer.Create(self);
        Ti.OnTimer := tionTimer;
        Ti.Interval := 1000;
        Ti.Enabled := False;
        for i := 1 to 10 do
        begin
          Sleep(1000);
          Button2.Caption := IntToStr(i);
          if i > 5 then
            Ti.Enabled := True;
        end;
      end;
    end;
      

  6.   

    正如 kiboisme 所说,timer 在主线程运行,会把程序搞得很沉重,用户体验不好。我一般是用自己写的线程时钟,很简单几行代码,而且可以同时设置多个时间触发点。
      

  7.   

    sorry,随口一答差点误导了别人
      

  8.   

    http://blog.csdn.net/yu444/article/details/5939103#t12
      

  9.   

    timer是一个低等级时间,如楼上所说,要么等待,要么丢弃。