首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

VCL线程同步的有关问题

2012-02-16 
VCL线程同步的问题unit Unit1interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics,

VCL线程同步的问题
unit Unit1;

interface

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

type
  TMyThread = class(TThread)
  private
  procedure SetVisible;
  protected
  procedure Execute;override;
  end;

  TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  procedure FormCreate(Sender: TObject);
  procedure Button1Click(Sender: TObject);
  private
  { Private declarations }
  FThread: TMyThread;
  public
  { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyThread }

procedure TMyThread.Execute;
begin
  Form1.Button2.Visible := True;
  //Synchronize(SetVisible);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Button2.Visible := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FThread := TMyThread.Create(False);
end;

procedure TMyThread.SetVisible;
begin
  Form1.Button2.Visible := True;
end;

end.



上面的代码Button2控件对应的windows对象会在线程结束时,无缘无故被destroy,导致窗口关闭时报错“句柄无效”。

这是为什么?使用同步却不会。

请讲的详细一点。

[解决办法]
我调试了一下,发现Form1.Button2.Visible := True;主要是调用代码:

Delphi(Pascal) code
procedure TControl.SetVisible(Value: Boolean);begin  if FVisible <> Value then  begin    VisibleChanging;    FVisible := Value;    Perform(CM_VISIBLECHANGED, Ord(Value), 0);    RequestAlign;  end;end; 

热点排行