关于Synchronize(50分)

K

kenmen

Unregistered / Unconfirmed
GUEST, unregistred user!
有这个程序,是关于线程的:
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Paintth;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
PT: TPainterThread;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
Button2.Enabled := True;
PT := TPainterThread.Create (False);
// start
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
PT.Free;
Button1.Enabled := True;
Button2.Enabled := False;
end;

procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
Canvas.Pen.Color := Color;
// of the form
Canvas.Brush.Color := Color;
Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
end;

end.


unit paintth;
interface
uses
Classes;
type
TPainterThread = class(TThread)
private
X, Y: Integer;
protected
procedure Execute;
override;
procedure Paint;
end;

implementation
{ TPainterThread }
uses
MainForm, Graphics;
procedure TPainterThread.Paint;
begin
Form1.Canvas.Pixels [X, Y] := clRed;
end;

procedure TPainterThread.Execute;
begin
Randomize;
repeat
X := Random (300);
Y := Random (Form1.ClientHeight);
Synchronize (Paint);
until Terminated;
end;

end.
我想问,unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Paintth;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
PT: TPainterThread;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
Button2.Enabled := True;
PT := TPainterThread.Create (False);
// start
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
PT.Free;
Button1.Enabled := True;
Button2.Enabled := False;
end;

procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
Canvas.Pen.Color := Color;
// of the form
Canvas.Brush.Color := Color;
Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
end;

end.


unit paintth;
interface
uses
Classes;
type
TPainterThread = class(TThread)
private
X, Y: Integer;
protected
procedure Execute;
override;
procedure Paint;
end;

implementation
{ TPainterThread }
uses
MainForm, Graphics;
procedure TPainterThread.Paint;
begin
Form1.Canvas.Pixels [X, Y] := clRed;
end;

procedure TPainterThread.Execute;
begin
Randomize;
repeat
X := Random (300);
Y := Random (Form1.ClientHeight);
Synchronize (Paint);
until Terminated;
end;

end.
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Paintth;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
PT: TPainterThread;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
Button2.Enabled := True;
PT := TPainterThread.Create (False);
// start
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
PT.Free;
Button1.Enabled := True;
Button2.Enabled := False;
end;

procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
Canvas.Pen.Color := Color;
// of the form
Canvas.Brush.Color := Color;
Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
end;

end.


unit paintth;
interface
uses
Classes;
type
TPainterThread = class(TThread)
private
X, Y: Integer;
protected
procedure Execute;
override;
procedure Paint;
end;

implementation
{ TPainterThread }
uses
MainForm, Graphics;
procedure TPainterThread.Paint;
begin
Form1.Canvas.Pixels [X, Y] := clRed;
end;

procedure TPainterThread.Execute;
begin
Randomize;
repeat
X := Random (300);
Y := Random (Form1.ClientHeight);
Synchronize (Paint);
until Terminated;
end;

end.
我想问:Synchronize 有啥用?我去掉它程序一样可运行。
还有,怎样知一个线程是主线程?
请高手们详细点,我对线程还是初鸟!!!!!
 
Synchronize可以让线程的一些方法在主线程中执行.
 
各位大虾能按着程序说说吗?
 
《delphi5开发人员指南》中有详细解答
 
Synchronize 方法主要是用于同步 VCL 的代码,因为 VCL 不是线程安全的,使用 Synchronize
可以解决 VCL 的并发冲突,由于并发冲突发生的几率很小,所以你去掉它暂时不会发现问题。
 
能说说啥是同步 VCL 吗?
 
还有,我想问问什么是线程安全,什么是线程重入?
谢谢了!!!
 
虽然答应你把这个问题回答了,但泛泛的解释对你是不够的
同意:caizongjie523
《delphi5开发人员指南》中有详细解答
郭玉梁和BaKuBaKu的解释也是对的,希望你翻一下书
 
那《delphi5开发人员指南》在那有的买?
我那没得买。。。
 
delphi5开发人员指南 138元
www.dangdang.com 有卖,我那时是五折买的,送货上门,不知道现在多少钱了。
如果你在教育网,可以到这里下载电子版,要不就免了。
ftp://ftp.xjtu.edu.cn/pub/document/china-pub/chinapub_unix_programming/Programming/Delphi 5开发人员指南
 
to wjiachun
能說說它的具體内容嗎?
 
我买了!
 

Similar threads

I
回复
0
查看
511
import
I
I
回复
0
查看
658
import
I
I
回复
0
查看
632
import
I
顶部