初学线程,请赐教--急!在线等待(100分)

  • 主题发起人 主题发起人 crazyworker
  • 开始时间 开始时间
procedure threadone.Execute
begin
Synchronize(UpdateCaption);
end;
procedure Tmytread.somefun;
begin
Form4.*** := '***';//做你想做的
end;
 
呵呵···俺初学线程···真是不好意思。
whtsuperant,Synchronize(somefun),somefun是什么?线程吗?
 
whtsuperant,somefun在哪里定义?线程的unit内吗?
 
somefun 是线程的一个方法,不能带有任何参数。
在这个方法中对TForm等类的成员进行访问,somefun中的代码实际上是在主线程中执行的.
 
是一个函数
你这样就可以了
procedure threadone.execute;
begin
Synchronize(somefun);
end;
procedure threadone.somfun;
var
i,j,count,count2:integer;
begin
begin
{ Place thread code here }
//用hopfield网络计算选号;
///初始化
for i:=1 to 31do
for j:=1 to 31do
form4.distance[i,j]:=1-form4.distance[i,j];
randomize();
form4.caluWij();
for i:=1 to 31do
for j:=1 to 31do
begin
form4.S[i,j]:=0+random(10)/10000;
form4.Uc[i,j]:=0;
end;
count:=1;
count2:=1;
form4.randomRan(961,961);
for i:=1 to 961do
form4.ran:=form4.ran-1;
form9.Show;
////////////////////////////
repeat
if Terminated then
break;
if count=962 then
begin
form4.randomRan(961,961);
for i:=1 to 961do
form4.ran:=form4.ran-1;
count:=1;
end;
i:=(form4.ran[count] mod 31 )+1;//取余+1作为个位
j:=(form4.ran[count] div 31 )+1;//取整+1作为十位
form4.updateUc(j,i);
if form4.Uc[j,i]>0
then
form4.S[j,i]:=1
else
form4.S[j,i]:=0;
form4.caluE1();
form4.caluE2();
form4.caluE3();
form9.label3.caption:=inttostr(count);
if form4.S[i,j]=1 then
begin
form9.canvas.brush.Style := bsSolid;
form9.canvas.brush.Color := clBlack;
form9.Canvas.Ellipse((20+20*(j-1)),
(20+20*(i-1)),(30+20*(j-1)),(30+20*(i-1)));
end
else
if form4.S[i,j]=0 then
begin
form9.canvas.brush.Style := bsSolid;
form9.canvas.brush.Color := clwhite;
form9.Canvas.Ellipse((20+20*(j-1)),
(20+20*(i-1)),(30+20*(j-1)),(30+20*(i-1)));
end;
count:=count+1;
count2:=count2+1;
until
((form4.E1=0) and (form4.E2=0) and (form4.E3=0))
showmessage('计算完毕。');
for j:=1 to 31do
if form4.S[j,1]=1 then
form4.suiEdit8.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,2]=1 then
form4.suiEdit9.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,3]=1 then
form4.suiEdit10.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,4]=1 then
form4.suiEdit11.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,5]=1 then
form4.suiEdit12.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,6]=1 then
form4.suiEdit13.Text :=inttostr(j);
for j:=1 to 31do
if form4.S[j,7]=1 then
form4.suiEdit14.Text :=inttostr(j);
if form4.fie=8 then
begin
for j:=1 to 31do
if form4.S[j,8]=1 then
form4.suiEdit24.Text :=inttostr(j);
end
else
form4.suiEdit24.Text:='';
end;
end;
 
procedure threadone.Execute
begin
Synchronize(UpdateCaption);
end;
procedure Tmytread.somefun;
begin
Form4.*** := '***';//做你想做的
end;

还有UpdateCaption什么意思?
 
ok
我试试,行的话马上给分。谢谢各位了。
 
我写错了,就该是这样:
procedure threadone.Execute
begin
Synchronize(somefun);
end;
procedure threadone.somefun;
begin
Form4.*** := '***';//做你想做的
end;

 
还有,我要如何调用线程?
threadA:=threadone.create(false)
这样是不是不行啊?
这样呢?:
threadA:=threadone.create(true)
threadA.FreeOnTerminate:=true;
threadA.Resume;

 
我试了一下,编译说somefun未声明,要在哪里声明somefun?
 
在主进程中:
threada:=threadone.create(false);

var
threada:Tmytread ;
begin
threada:=Tmytread.Create(false);
//false马上执行,true先挂起
threada.Resume;//回复执行挂起的线程
threada.Suspend;
//挂起线程
end;
 
靠,大哥你是不是连Delphi也是初学呀
当然是在你的线程类中声明了
 
搞定了,给分
 
后退
顶部