
19.02.2011, 16:27
|
 |
Let Me Show You
|
|
Регистрация: 30.04.2010
Адрес: Северодвинск
Сообщения: 5,426
Версия Delphi: 7, XE5
Репутация: 59586
|
|
TCriticalSection должна быть глобальной:
Код:
unit Unit1;
interface
uses
SyncObjs,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls;
type
TMyThread = class(TThread)
private
procedure Avatar;
protected
procedure Execute; override;
public
i: Integer;
constructor Create;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
cs: TCriticalSection;
implementation
{$R *.dfm}
{ TMyThread }
procedure TMyThread.Avatar;
begin
Form1.Memo1.Lines.Add(IntToStr(i));
end;
constructor TMyThread.Create;
begin
inherited Create(True);
FreeOnTerminate:=True;
end;
procedure TMyThread.Execute;
begin
cs.Acquire;
try
Sleep(i);
finally
cs.Release;
end;
Synchronize(Avatar);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TMyThread.Create do
begin
i:=2000;
Resume;
Sleep(100);
end;
with TMyThread.Create do
begin
i:=1000;
Resume;
Sleep(100);
end;
with TMyThread.Create do
begin
i:=500;
Resume;
Sleep(100);
end;
end;
initialization
cs:=TCriticalSection.Create;
finalization
cs.Free;
end.
__________________
Пишу программы за еду.
__________________
|