在constructor过程中,是否可以加入synchronize()过程?我加入时老死锁。即使它调用的是一个i:=1000;的简单过程也死锁。且,用trackbar在线程执行时分配优先也出错。能帮我解决这两个问题吗?源代码以下:
constructor TMythread.createit(prioritylevel:cardinal;moveimage:Timage;
xs,xe:integer;dir:Tdirection);
begin
inherited create(true);
priority:=TThreadpriority(prioritylevel);
freeonTerminate:=true;
image:=moveimage;
x_start:=xs;
x_end:=xe;
direction:=dir;
synchronize(initmove);
suspended:=false;
end;
destructor TMyThread.destroy;
begin
postmessage(w_main.handle,wm_threaddonemsg,self.threadid,0);
inherited destroy;
end;
procedure TMythread.Execute;
var
i:cardinal;
begin
{ Place thread code here }
i:=1;
while ((Terminated=false) and (i<1000000)) do
begin
synchronize(updatemove);
inc(i);
end;
end;procedure TMythread.initmove;
var
i:integer;
begin
{ if direction=left_to_right then
image.left:=x_start
else
image.left:=x_end-image.width;
}
i:=1000;
end;
procedure TMyThread.updatemove;
begin
if direction=left_to_right then
if image.left<(x_end-image.width) then
image.left:=image.left+1
else
direction:=right_to_left
else
if image.left>x_start then
image.left:=image.left-1
else
direction:=left_to_right;
end;
end.
constructor TMythread.createit(prioritylevel:cardinal;moveimage:Timage;
xs,xe:integer;dir:Tdirection);
begin
inherited create(true);
priority:=TThreadpriority(prioritylevel);
freeonTerminate:=true;
image:=moveimage;
x_start:=xs;
x_end:=xe;
direction:=dir;
synchronize(initmove);
suspended:=false;
end;
destructor TMyThread.destroy;
begin
postmessage(w_main.handle,wm_threaddonemsg,self.threadid,0);
inherited destroy;
end;
procedure TMythread.Execute;
var
i:cardinal;
begin
{ Place thread code here }
i:=1;
while ((Terminated=false) and (i<1000000)) do
begin
synchronize(updatemove);
inc(i);
end;
end;procedure TMythread.initmove;
var
i:integer;
begin
{ if direction=left_to_right then
image.left:=x_start
else
image.left:=x_end-image.width;
}
i:=1000;
end;
procedure TMyThread.updatemove;
begin
if direction=left_to_right then
if image.left<(x_end-image.width) then
image.left:=image.left+1
else
direction:=right_to_left
else
if image.left>x_start then
image.left:=image.left-1
else
direction:=left_to_right;
end;
end.
所以同步处理只使用TMythread.Execute中.
你的那线程对象建构是在主线程中. =====哈欠====halfdream====
while ((Terminated=false) and (i<1000000)) do
begin
synchronize(updatemove);//问题在这儿.
//你的updateMove调用了TImage
//使用同步方法的时候,会让其它线程
//即主线程不能更新Image
inc(i);
请在这儿加上一个sleep(100);
//让主线程有时间去更新界面.
end;
=====哈欠====halfdream====
procedure Tw_main.trackbar1change(sender:Tobject);
begin
if (mythread1<>nil) and (thread1active<>true) then
mythread1.priority:=TThreadpriority(trackbar1.position);
end;
begin
if (mythread1<>nil) then
begin
mythread1.suspend;
if (thread1active<>true) then
mythread1.priority:=TThreadpriority(trackbar1.position);
mythread1.resume;
end;
end;
在主程序中出现:
procedure threaddone(var amessage:Tmessage);message WM_threadDoneMsg;
实现以下:
procedure Tw_main.Threaddone(var amessage:Tmessage);
begin
if ((mythread1<>nil) and (mythread1.threadid=cardinal(amessage.wparam))) then
begin
thread1active:=false;
end;
if ((mythread2<>nil) and (mythread2.threadid=cardinal(amessage.wparam))) then
begin
thread2active:=false;
end;
end;
请问这过程有什么用?它除了信息机制外,从不被调用。当我直接close关闭主程序时,它显示句柄错误,为什么?