G
gztiger
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
iCount := 0;
listbox1.Items.Clear;
with ThreadT1.create(100, 'A') do
waitfor;
ThreadT1.create(100, 'B');
end;
end.
-------------------------------------------------------
unit Unit2;
interface
uses
Classes, SysUtils, unit1;
type
ThreadT1 = class(TThread)
private
FiMax: integer;
Foption: string;
Fi: integer;
{ Private declarations }
protected
procedure Execute; override;
procedure AddItem();
public
constructor create(iMax: integer; option: string);
end;
implementation
{ ThreadT1 }
procedure ThreadT1.AddItem;
begin
inc(Fi);
Form1.ListBox1.Items.Add(format('item%s %d', [Foption, Fi]));
end;
constructor ThreadT1.create(iMax: integer; option: string);
begin
FiMax := iMax;
Foption := option;
FreeOnTerminate := true;
inherited create(false);
end;
procedure ThreadT1.Execute;
var
i: integer;
begin
for i := 0 to fimax - 1 do
begin
sleep(50);
Synchronize(AddItem)
end;
end;
end.
-------------------------------
代码很简单,本来是想ThreadT1.create(100, 'A')执行完后再执行ThreadT1.create(100, 'B'),但是运行报错。不知是怎么回事。大家帮忙看看。先谢过了。
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
iCount := 0;
listbox1.Items.Clear;
with ThreadT1.create(100, 'A') do
waitfor;
ThreadT1.create(100, 'B');
end;
end.
-------------------------------------------------------
unit Unit2;
interface
uses
Classes, SysUtils, unit1;
type
ThreadT1 = class(TThread)
private
FiMax: integer;
Foption: string;
Fi: integer;
{ Private declarations }
protected
procedure Execute; override;
procedure AddItem();
public
constructor create(iMax: integer; option: string);
end;
implementation
{ ThreadT1 }
procedure ThreadT1.AddItem;
begin
inc(Fi);
Form1.ListBox1.Items.Add(format('item%s %d', [Foption, Fi]));
end;
constructor ThreadT1.create(iMax: integer; option: string);
begin
FiMax := iMax;
Foption := option;
FreeOnTerminate := true;
inherited create(false);
end;
procedure ThreadT1.Execute;
var
i: integer;
begin
for i := 0 to fimax - 1 do
begin
sleep(50);
Synchronize(AddItem)
end;
end;
end.
-------------------------------
代码很简单,本来是想ThreadT1.create(100, 'A')执行完后再执行ThreadT1.create(100, 'B'),但是运行报错。不知是怎么回事。大家帮忙看看。先谢过了。