关于几个线程同步的问题,多谢(50)

  • 主题发起人 主题发起人 himoo
  • 开始时间 开始时间
H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
比如两个线程:procedure TForm1.BitBtn1Click(Sender: TObject);begin TBounceThread.Create(False,Shape2,2,3);end;procedure TForm1.BitBtn2Click(Sender: TObject);begin TMyCaluThread.Create(False);end;2个都是对主界面控件的操作,如变换位置,更改标题等。单独点执行任一个,完美执行。但是如果,先点一个,在其运行时,点了第二个,就会卡死,如何解决?谢谢
 
这个不是问题,关键是你在线程中访问了有消息控制的Form,必须用Syn...同步调用语句调用Form中的控件!
 
操作主线程的东西要调用同步函数Synchronize,具体使用方法可以百度一下就明白了
 
1.用Synchronize,同步主线程与子线程2.当多个子线程抢夺主线程资源时,可通过临界区,互斥对象,信号量来避免冲突,其中最为简单的就是用临界区,详细用法可以参考万一老师的blog,写得非常清楚非常好,如下:http://www.cnblogs.com/del/category/174761.html
 
好的,多谢以上几位,我在看了,就是求个例子~我怕看那个看不懂。。。~有劳了
 
例如:procedure tform1.setleft;begin form1.left:=8;end;procedute YouThreadDo;begin Synchronize(form1.setleft);end;
 
不行啊-----------------procedure TForm1.BitBtn1Click(Sender: TObject);begin //TBounceThread.Create(False,Shape1,1,2)
TBounceThread.Create(False,Shape2,2,3);end;procedure TForm1.BitBtn2Click(Sender: TObject);begin TBounceThread.Create(False,Shape3,2,3);end;----------------- 依次按2个按钮,卡死,只按一个没问题; BitBtn1Click里面同时运行2个一样不行。 这个是几个图形移动的。TBounceThread单元如下:unit Unit_Move;interfaceuses 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)
property Shape:TShape read FShape
end;implementationprocedure TBounceThread.MoveShape;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
FYSpeed:=YSpeed
FreeOnTerminate:=True;end;end.
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Button1: TButton
Button2: TButton
Shape1: TShape
Shape2: TShape
Shape3: TShape
procedure Button1Click(Sender: TObject)
procedure Button2Click(Sender: TObject)
procedure FormCreate(Sender: TObject)
procedure FormClose(Sender: TObject
var Action: TCloseAction)
private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses Unit_Move;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin TBounceThread.Create(False, Shape2, 2, 3);end;procedure TForm1.Button2Click(Sender: TObject);begin TBounceThread.Create(False, Shape3, 2, 3);end;procedure TForm1.FormCreate(Sender: TObject);begin InitializeCriticalSection(CriticalSection);end;procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);begin DeleteCriticalSection(CriticalSection);end;end.unit Unit_Move;interfaceuses 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)
property Shape: TShape read FShape
destructor Destroy
override
end;var CriticalSection: TRTLCriticalSection;implementationprocedure TBounceThread.MoveShape;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 EnterCriticalSection(CriticalSection)
MoveShape
LeaveCriticalSection(CriticalSection)
// Synchronize(MoveShape)
end;end;constructor TBounceThread.Create(Suspended: Boolean
Shape: TShape
XSpeed, YSpeed: Integer);begin inherited Create(Suspended)
FShape := Shape
FXSpeed := XSpeed
FYSpeed := YSpeed
FreeOnTerminate := True;end;destructor TBounceThread.Destroy;begin inherited;end;end.
 
还有一个就是 TBounceThread.Create(False,Shape2,2,3)
之后 如何手动停止它?
 
你要这样写。var BounceThread: TBounceThread;begin BounceThread := TBounceThread.Create(False, Shape3, 2, 3)
// BounceThread.Terminate
BounceThread.Destroy;
 
嗯,停止没问题了,谢谢了。2个一起的还不行[:(]
 
2个一起的还不行?你要达到什么效果。 我这里测试没有死机啊
 
BounceThread.Terminate;可以使之停止移动了,BounceThread.Destroy
却报句柄无效6。咋回事?现在也不会卡死了。学会用临界区了,非常感谢!
 
BounceThread.Destroy
却报句柄无效6。咋回事? 再解答一下啊。。。[:(]
 
FreeOnTerminate 默认是 true,所有不需要 BounceThread.Destroy
//BounceThread.Destroy;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
724
import
I
后退
顶部