为了验证一个小问题,结果被搞糊涂了。 ( 积分: 50 )

  • 主题发起人 主题发起人 masm
  • 开始时间 开始时间
M

masm

Unregistered / Unconfirmed
GUEST, unregistred user!
请看下面简单代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
L:TThreadList;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clGreen;
Finally
L.UnlockList;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clRed;
Finally
L.UnlockList;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
L:=TThreadList.Create;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
L.LockList;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
L.UnlockList;
end;

procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
L.Free;
end;
end.
=========================================
我的预期:Button3把L加锁,进入临界区,Button1加锁时应等待L解锁再加锁进入。即按Button3后,再按Button1,Label1的颜色不作任何改变,按Button4.解锁后,Label1颜色改变。
结果不是这样,跟加不加锁好像没关系,L居然纯粹多余。郁闷!!
 
请看下面简单代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
L:TThreadList;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clGreen;
Finally
L.UnlockList;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clRed;
Finally
L.UnlockList;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
L:=TThreadList.Create;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
L.LockList;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
L.UnlockList;
end;

procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
L.Free;
end;
end.
=========================================
我的预期:Button3把L加锁,进入临界区,Button1加锁时应等待L解锁再加锁进入。即按Button3后,再按Button1,Label1的颜色不作任何改变,按Button4.解锁后,Label1颜色改变。
结果不是这样,跟加不加锁好像没关系,L居然纯粹多余。郁闷!!
 
后退
顶部