编
编程菜鸟2008
Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下
用两个Listbox分别来显示1-100和2-200,用两个线程控制,在他们之间创建了互斥关系,但是运行后两个显示的反而相同,不知道为什么,请高手指点
unit thread3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;
type
TSyncThread=class(TThread)
protected
procedure execute;override;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1,Thread2:TSyncThread;
procedure Thread1done(sender:TObject);
procedure Thread2done(sender:Tobject);
{ Private declarations }
public
{ Public declarations }
end;
const maxsize=100;
var
Form1: TForm1;
Globaldata:array[1..maxsize] of integer;
Incnum:integer=0;
Hmutex:Thandle=0;
implementation
{$R *.dfm}
procedure TsyncThread.execute;
var
i:integer;
waitreturnWORD;
begin
FreeOnTerminate:=true;
waitreturn:=WaitForsingleObject(Hmutex,INFINITE);
if waitreturn=Wait_Object_0 then
for i:=1 to maxsizedo
begin
Inc(Incnum);
Globaldata:=Incnum;
sleep(10);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Hmutex:=CreateMutex(nil,false,nil);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
closehandle(Hmutex);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1:=TsyncThread.Create(false);
Thread1.OnTerminate:=Thread1done;
Thread2:=TsyncThread.Create(false);
Thread2.OnTerminate:=Thread2done;
end;
procedure Tform1.Thread1done(sender:Tobject);
var
i:integer;
begin
listbox1.Items.Clear;
for i:=1 to maxsizedo
listbox1.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
procedure Tform1.Thread2done(sender:Tobject);
var
i:integer;
begin
listbox2.Items.Clear;
for i:=1 to maxsizedo
listbox2.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
end.
请帮忙,初次发贴
用两个Listbox分别来显示1-100和2-200,用两个线程控制,在他们之间创建了互斥关系,但是运行后两个显示的反而相同,不知道为什么,请高手指点
unit thread3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;
type
TSyncThread=class(TThread)
protected
procedure execute;override;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1,Thread2:TSyncThread;
procedure Thread1done(sender:TObject);
procedure Thread2done(sender:Tobject);
{ Private declarations }
public
{ Public declarations }
end;
const maxsize=100;
var
Form1: TForm1;
Globaldata:array[1..maxsize] of integer;
Incnum:integer=0;
Hmutex:Thandle=0;
implementation
{$R *.dfm}
procedure TsyncThread.execute;
var
i:integer;
waitreturnWORD;
begin
FreeOnTerminate:=true;
waitreturn:=WaitForsingleObject(Hmutex,INFINITE);
if waitreturn=Wait_Object_0 then
for i:=1 to maxsizedo
begin
Inc(Incnum);
Globaldata:=Incnum;
sleep(10);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Hmutex:=CreateMutex(nil,false,nil);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
closehandle(Hmutex);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1:=TsyncThread.Create(false);
Thread1.OnTerminate:=Thread1done;
Thread2:=TsyncThread.Create(false);
Thread2.OnTerminate:=Thread2done;
end;
procedure Tform1.Thread1done(sender:Tobject);
var
i:integer;
begin
listbox1.Items.Clear;
for i:=1 to maxsizedo
listbox1.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
procedure Tform1.Thread2done(sender:Tobject);
var
i:integer;
begin
listbox2.Items.Clear;
for i:=1 to maxsizedo
listbox2.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
end.
请帮忙,初次发贴