A
abencat
Unregistered / Unconfirmed
GUEST, unregistred user!
---窗体
object Form1: TForm1
Left = 212
Top = 46
Width = 432
Height = 694
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button2: TButton
Left = 327
Top = 40
Width = 75
Height = 25
Caption = '停止'
TabOrder = 0
end
object Button3: TButton
Left = 324
Top = 7
Width = 75
Height = 25
Caption = '开始'
TabOrder = 1
OnClick = Button3Click
end
object StringGrid1: TStringGrid
Left = 0
Top = 0
Width = 304
Height = 667
Align = alLeft
RowCount = 100
TabOrder = 2
end
object Edt_N: TSpinEdit
Left = 332
Top = 78
Width = 68
Height = 22
MaxValue = 0
MinValue = 0
TabOrder = 3
Value = 10
end
end
---代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Grids, Spin;
type
TForm1 = class(TForm)
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
Edt_N: TSpinEdit;
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure TestOK(sender: TObject);
end;
var
Form1: TForm1;
implementation
uses BounceThread;
var
P: array of TBounceThread;
{$R *.DFM}
procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
begin
SetLength(P,Edt_N.Value);
InitializeCriticalSection(CS);
//初始化临界区
for i:=0 to Edt_N.Value-1do
begin
P:=TBounceThread.Create(true);
P.Resume;
end;
end;
procedure TForm1.TestOK(sender: TObject);
begin
end;
end.
---
unit BounceThread;
interface
uses
Windows, Messages, SysUtils, WinProcs,Classes,Graphics,ExtCtrls,unit1;
var
CS: TRTLCriticalSection;
//定义全局临界区
type
TbounceThread = class(TThread)
private
iCur:integer;
procedure MoveShape();
protected
procedure Execute;override;
public
constructor Create(Suspended:Boolean);
end;
implementation
constructor TBounceThread.Create(Suspended:Boolean);
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
end;
procedure TbounceThread.MoveShape();
var
i:integer;
begin
Form1.StringGrid1.Cells[0,iCur]:='OK';
for i:=0 to 10do
begin
Form1.StringGrid1.Cells[2,iCur]:=inttostr(i);
sleep(300);
end;
end;
procedure TbounceThread.Execute;
var
i:integer;
begin
for i:=0 to Form1.StringGrid1.RowCount-1do
begin
if Form1.StringGrid1.Cells[0,i]='' then
begin
//开始运行一个过程 做事情
iCur:=i;
MoveShape;
//synchronize(MoveShape);
end;
//结束线程
if i=Form1.StringGrid1.RowCount-1 then
Terminate ;
end;
end;
end.
object Form1: TForm1
Left = 212
Top = 46
Width = 432
Height = 694
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button2: TButton
Left = 327
Top = 40
Width = 75
Height = 25
Caption = '停止'
TabOrder = 0
end
object Button3: TButton
Left = 324
Top = 7
Width = 75
Height = 25
Caption = '开始'
TabOrder = 1
OnClick = Button3Click
end
object StringGrid1: TStringGrid
Left = 0
Top = 0
Width = 304
Height = 667
Align = alLeft
RowCount = 100
TabOrder = 2
end
object Edt_N: TSpinEdit
Left = 332
Top = 78
Width = 68
Height = 22
MaxValue = 0
MinValue = 0
TabOrder = 3
Value = 10
end
end
---代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Grids, Spin;
type
TForm1 = class(TForm)
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
Edt_N: TSpinEdit;
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure TestOK(sender: TObject);
end;
var
Form1: TForm1;
implementation
uses BounceThread;
var
P: array of TBounceThread;
{$R *.DFM}
procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
begin
SetLength(P,Edt_N.Value);
InitializeCriticalSection(CS);
//初始化临界区
for i:=0 to Edt_N.Value-1do
begin
P:=TBounceThread.Create(true);
P.Resume;
end;
end;
procedure TForm1.TestOK(sender: TObject);
begin
end;
end.
---
unit BounceThread;
interface
uses
Windows, Messages, SysUtils, WinProcs,Classes,Graphics,ExtCtrls,unit1;
var
CS: TRTLCriticalSection;
//定义全局临界区
type
TbounceThread = class(TThread)
private
iCur:integer;
procedure MoveShape();
protected
procedure Execute;override;
public
constructor Create(Suspended:Boolean);
end;
implementation
constructor TBounceThread.Create(Suspended:Boolean);
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
end;
procedure TbounceThread.MoveShape();
var
i:integer;
begin
Form1.StringGrid1.Cells[0,iCur]:='OK';
for i:=0 to 10do
begin
Form1.StringGrid1.Cells[2,iCur]:=inttostr(i);
sleep(300);
end;
end;
procedure TbounceThread.Execute;
var
i:integer;
begin
for i:=0 to Form1.StringGrid1.RowCount-1do
begin
if Form1.StringGrid1.Cells[0,i]='' then
begin
//开始运行一个过程 做事情
iCur:=i;
MoveShape;
//synchronize(MoveShape);
end;
//结束线程
if i=Form1.StringGrid1.RowCount-1 then
Terminate ;
end;
end;
end.