L
Liyue
Unregistered / Unconfirmed
GUEST, unregistred user!
请大虾帮助修改以下多线程例子,谢谢!:
主要问题:
1."if Terminated then
..." 语句似乎没有什么语法问题,但运行时总是出错。为什么?
2.线程运行结束后,会运行一个过程"UUU".这样,当两个线程刚好“同时”结束时,就会同时调用"UUU",引起程序运行结果的变化。怎样改进?
unit Unit1;
interface
uses
UnitBncThred,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit4: TEdit;
Edit5: TEdit;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Edit6: TEdit;
Edit7: TEdit;
procedure Button1Click(Sender: TObject);
procedure uuu(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i,W:integer;
n:array[1..2] of integer;
p:array[1..2] of mythread;
e:array[1..10] of tedit;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
n[1]:=0;n[2]:=0;
e[1]:=edit1;e[2]:=edit2;e[3]:=edit3;e[4]:=edit4;
e[5]:=edit5;e[6]:=edit6;e[7]:=edit7;
for i:=1 to 2do
begin
w:=5*i;n:=n+1;
e[2+i].text:=inttostr(w);
e[4+i].text:=inttostr(n);
e[7].text:=inttostr(n[1]+n[2]);
p:=myThread.Create(False,e,w);
p.onterminate:=uuu;
end;
end;
procedure TForm1.uuu(Sender: TObject);
//当两个线程刚好同时调用UUU过程时,会发生混乱。怎么改进?//
begin
if n[1]+n[2]>8 then
exit;
for i:=1 to 2do
if p.Terminated then
begin
//运行时显示"未说明标示符:'terminated'",难道terminated不是delphi的保留字吗?!或者我的delphi5有问题!?//
n:=n+1;
e[4+i].text:=inttostr(n);
e[7].text:=inttostr(n[1]+n[2]);
p:=myThread.Create(False,e,w);
p.onterminate:=uuu;
end;
end;
end.
unit UnitBncThred;
interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, StdCtrls, ExtCtrls;
Type
mythread=Class(TThread)
Private
aedit:Tedit;
p:integer;
a:Integer;
b:Integer;
Procedure disloop;
Protected
procedure Execute;Override;
public
Constructor Create(Suspended:Boolean;edit:Tedit;p:integer);
end;
implementation
Procedure mythread.disloop;
begin
aedit.text:=inttostr(b);
end;
Procedure myThRead.execute;
var
i:integer;
begin
for i:=1 to ado
begin
b:=i;
synchronize(disloop);
sleep(1000);
if TerMinated then
exit;
end;
end;
Constructor mythread.Create(Suspended:Boolean;edit:Tedit;p: integer);
begin
inherited Create(Suspended);
aedit:=edit;
a:=p;
FreeOnTerminate:=True;
end;
end.
主要问题:
1."if Terminated then
..." 语句似乎没有什么语法问题,但运行时总是出错。为什么?
2.线程运行结束后,会运行一个过程"UUU".这样,当两个线程刚好“同时”结束时,就会同时调用"UUU",引起程序运行结果的变化。怎样改进?
unit Unit1;
interface
uses
UnitBncThred,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit4: TEdit;
Edit5: TEdit;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Edit6: TEdit;
Edit7: TEdit;
procedure Button1Click(Sender: TObject);
procedure uuu(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i,W:integer;
n:array[1..2] of integer;
p:array[1..2] of mythread;
e:array[1..10] of tedit;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
n[1]:=0;n[2]:=0;
e[1]:=edit1;e[2]:=edit2;e[3]:=edit3;e[4]:=edit4;
e[5]:=edit5;e[6]:=edit6;e[7]:=edit7;
for i:=1 to 2do
begin
w:=5*i;n:=n+1;
e[2+i].text:=inttostr(w);
e[4+i].text:=inttostr(n);
e[7].text:=inttostr(n[1]+n[2]);
p:=myThread.Create(False,e,w);
p.onterminate:=uuu;
end;
end;
procedure TForm1.uuu(Sender: TObject);
//当两个线程刚好同时调用UUU过程时,会发生混乱。怎么改进?//
begin
if n[1]+n[2]>8 then
exit;
for i:=1 to 2do
if p.Terminated then
begin
//运行时显示"未说明标示符:'terminated'",难道terminated不是delphi的保留字吗?!或者我的delphi5有问题!?//
n:=n+1;
e[4+i].text:=inttostr(n);
e[7].text:=inttostr(n[1]+n[2]);
p:=myThread.Create(False,e,w);
p.onterminate:=uuu;
end;
end;
end.
unit UnitBncThred;
interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, StdCtrls, ExtCtrls;
Type
mythread=Class(TThread)
Private
aedit:Tedit;
p:integer;
a:Integer;
b:Integer;
Procedure disloop;
Protected
procedure Execute;Override;
public
Constructor Create(Suspended:Boolean;edit:Tedit;p:integer);
end;
implementation
Procedure mythread.disloop;
begin
aedit.text:=inttostr(b);
end;
Procedure myThRead.execute;
var
i:integer;
begin
for i:=1 to ado
begin
b:=i;
synchronize(disloop);
sleep(1000);
if TerMinated then
exit;
end;
end;
Constructor mythread.Create(Suspended:Boolean;edit:Tedit;p: integer);
begin
inherited Create(Suspended);
aedit:=edit;
a:=p;
FreeOnTerminate:=True;
end;
end.