Delphi的计时器问题

假如有一个操作是增加一个全局变量的值,但是还有一个计时器也用于定时增加该全局变量的值,那么假如在执行前一个操作的时候同时计时器也正好触发执行,这样会不会有冲突。

以下面的代码为例,假如在你点击按钮的时候同时计时器被触发会怎么样?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    tmr1: TTimer;
    procedure btn1Click(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i: Integer;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
  Inc(i);
  btn1.Caption:= IntToStr(i)

end;

procedure TForm1.tmr1Timer(Sender: TObject);
begin
  Inc(i);
  btn1.Caption:= IntToStr(i);

end;

end.
阅读 3.6k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏