请您解答:delphi多线程例程

liyue 2000-07-08 08:54:00
我在本网的delphi开发文章中看到《delphi32下的多线程程序设计》一文。但其给的例程无法运行,我已经“研究”了一夜,头都“大”了,请大虾指点。谢谢!一定要将程序在您的计算机上运行通过,然后照原样贴上来。我是初级水平,无法辨认任何“打印错误”。谢谢!例程如下:

  这是一个多线程的碰碰球游戏,你可以有多个不同的球,它们分属不同的线程,各自独立的在屏幕上碰撞。显然,由于多个球运行的显示会同时操作vcl资源,为了安
全,我们在execute过程中的执行部分加入了synchronize(moveshape)来调用moveshape过程,实际上,在任何需要操作vcl资源的地方,例如窗体、位图,都应加入synchronize调用。

  unit bncthrd;
  interface
  uses
  winprocs,classes,graphics,extctrls;
  type
  tbouncethread=class(tthread)
  private
  fshape:tshape;
  fxspeed:integer;
  fyspeed:integer;
  procedure moveshape;
  protected
  procedure execute;override;
  public
  constructor create(suspended:boolean;shape:tshape;xspeed,yspeed:integer);
  propertyshape:tshapereadfshape;
  end;
  implementation
  procedure tbouad.moveshape;
  var
  maxheight,maxwidth:integer;
  begin
  with fshape do
  begin
  left:=left+fxspeed;
  top:=top+fyspeed;
  if(left=0) or(left+width=parent.width)then
  fxspeed:=fxspeed*-1;
  if(top=0)or(top=height+parent.height)then
  fyspeed:=fyspeed*-1;
  end;
  end;
  procedure tbouncethread.execute;
  begin
  while not terminated do
  begin
  synchronize(moveshape);
  end;
  end;
  constructor tbouncethread.create(suspended:boolean;shape:tshape;
  xspeed,yspeed:integer);
  begin
  inherited create(suspended);
  fshape:=shape;
  fxspeed:=xspeed;{x轴走向的速度}
  fyspeed:=yspeed;{y轴走向的速度}
  freeonterminate:=true;
  end;
  end.

  执行时我们可以新建一个程序,然后在uses部分加入以上的bncthrd单元,再在它的窗体form1上加入两个shape控件shape1和shape2,shape1可以是一个矩形而shape2
是一个圆。加入以下的代码就可以让矩形和圆动起来。
unit Unit1;
interface
uses
bncthrd,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
tbouncethread.create(false,shape1,1,2);
  tbouncethread.create(false,shape2,2,3);
end;
end.
...全文
208 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyue 2000-07-08
  • 打赏
  • 举报
回复
程序已经调试通过,十分谢谢DXZ_pl,我把分数全部送给您。
dxz_pl 2000-07-08
  • 打赏
  • 举报
回复
unit Unit_1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, StdCtrls, ExtCtrls, ComCtrls, ToolWin;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
Uses UnitBncThred;

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
begin
TBounceThread.Create(False,Button1,1,2);
TBounceThread.Create(False,Button2,1,2);
end;

end.
//该程序在Delphi4、5中都已调试通过。
liyue 2000-07-08
  • 打赏
  • 举报
回复
先谢谢DXZ_pl,我马上试一试。
但怎样在程序中调用?请帖一个例程。谢谢!
liyue 2000-07-08
  • 打赏
  • 举报
回复
先谢谢DXZ_pl,我马上试一试。
dxz_pl 2000-07-08
  • 打赏
  • 举报
回复
//----线程
unit UnitBncThred;

interface

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, StdCtrls, ExtCtrls;

Type
TBounceThread=Class(TThread)

Private
FShape:TButton;
FXSpeed:Integer;
FYSpeed:Integer;
Procedure MoveShape;
Protected
procedure Execute;Override;
public
Constructor Create(Suspended:Boolean;Shape:TButton;XSpeed,YSpeed:integer);
End;

implementation

Procedure TBounceThRead.MoveShape;
Begin
With FShape Do
Begin
Left :=Left+FXSpeed;
Top :=Top+FYSpeed;
if (Left <0) or (Left +Width>Screen.Width) Then
FXSpeed:=FXSpeed*-1;
if (Top <0) or (Top+Height>Screen.Height) Then
FYSpeed:=FYSpeed*-1;
End;
End;

Procedure TBounceThRead.execute;
begin
While Not TerMinated do
Begin
Synchronize(moveShape);
End;
End;

Constructor TBounceThRead.Create(Suspended:Boolean;Shape:TButton;XSpeed,YSpeed:Integer);
Begin
inherited Create(Suspended);
FShape :=Shape;
FXSpeed :=XSpeed;
FYSpeed :=YSpeed;
FreeOnTerminate:=True;
End;
end.
//----
//----在程序中调用
TBounceThread.Create(False,Button1,1,2);
//----

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧