请教:如何通过进程中的一个全局变量控制两个子线程互斥?谢谢! ( 积分: 50 )

  • 主题发起人 主题发起人 chenshu_sc
  • 开始时间 开始时间
C

chenshu_sc

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序如下:
unit FormMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
th1 = class(TThread)
s : string;
FStatus : Integer;
FMemo : TMemo;
private
procedure w;
procedure showresult;
protected
procedure Execute ;
override;
public
constructor Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
end;

th2 = class(TThread)
s : string;
FStatus : Integer;
FMemo : TMemo;
private
procedure w;
procedure showresult;
protected
procedure Execute ;
override;
public
constructor Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
end;

TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
i : Integer;
t1 : th1;
t2 : th2;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure th1.w;
begin
try
if FStatus = 1 then
begin
FStatus := 2;
sleep(1000);
s := 'TH1';
FStatus := 1;
end;
finally
end;
end;

procedure th1.showresult;
begin
FMemo.Lines.Add(s);
end;

procedure th1.Execute ;
begin
while not terminateddo
begin
w;
Synchronize(showresult);
end;
end;

constructor th1.Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
FStatus := Status;
FMemo := mem;
end;

procedure th2.w;
begin
try
if FStatus = 1 then
begin
FStatus := 2;
sleep(300);
s := 'TH2';
FStatus := 1;
end;
finally
end;
end;

procedure th2.showresult;
begin
FMemo.Lines.Add(s);
end;

procedure th2.Execute ;
begin
while not terminateddo
begin
w;
Synchronize(showresult);
end;
end;

constructor th2.Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
begin
inherited create(suspended);
FreeOnTerminate:=True;
FStatus := Status;
FMemo := mem;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
i := 1;
t1 := th1.Create(false,i,memo1);
t2 := th2.Create(false,i,memo1);
end;

end.

很明显没有达到要求,但不知道该怎么改?请各位大大帮忙!
 
我的程序如下:
unit FormMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
th1 = class(TThread)
s : string;
FStatus : Integer;
FMemo : TMemo;
private
procedure w;
procedure showresult;
protected
procedure Execute ;
override;
public
constructor Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
end;

th2 = class(TThread)
s : string;
FStatus : Integer;
FMemo : TMemo;
private
procedure w;
procedure showresult;
protected
procedure Execute ;
override;
public
constructor Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
end;

TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
i : Integer;
t1 : th1;
t2 : th2;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure th1.w;
begin
try
if FStatus = 1 then
begin
FStatus := 2;
sleep(1000);
s := 'TH1';
FStatus := 1;
end;
finally
end;
end;

procedure th1.showresult;
begin
FMemo.Lines.Add(s);
end;

procedure th1.Execute ;
begin
while not terminateddo
begin
w;
Synchronize(showresult);
end;
end;

constructor th1.Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
begin
inherited Create(Suspended);
FreeOnTerminate:=True;
FStatus := Status;
FMemo := mem;
end;

procedure th2.w;
begin
try
if FStatus = 1 then
begin
FStatus := 2;
sleep(300);
s := 'TH2';
FStatus := 1;
end;
finally
end;
end;

procedure th2.showresult;
begin
FMemo.Lines.Add(s);
end;

procedure th2.Execute ;
begin
while not terminateddo
begin
w;
Synchronize(showresult);
end;
end;

constructor th2.Create(Suspended:Boolean ;
Status : Integer ;
mem : TMemo);
begin
inherited create(suspended);
FreeOnTerminate:=True;
FStatus := Status;
FMemo := mem;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
i := 1;
t1 := th1.Create(false,i,memo1);
t2 := th2.Create(false,i,memo1);
end;

end.

很明显没有达到要求,但不知道该怎么改?请各位大大帮忙!
 
晕,发了两个....
 
最终怎么实现的?说来听听哦[:D]
 
自由界面和报表的完美解决方案!
http://www.anylib.com
 
TO:lichengbin
  就是线程里的FStatus改成FORM.I就行了....不过不知道为什么将FStatus改成pointer的之后将地址指到FORM.I就不行....
 
接受答案了.
 
后退
顶部