我在主程序里显示数据,与在线程里显示数据时,主程序明显比线程显示数据要快,请问这是什么原因呢,有没有把线这时间缩短成主程序的一样呢(显示数据是同一个函数)
控件有:ListView1;Button1,Button2
主程序代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Unit2,
Dialogs, StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
ShowList:sh;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i :=1 to 1000 do
begin
with ListView1.Items.Add do //显示不
begin
Caption:=inttostr(i); //次序
SubItems.Add(floattostr(i));
SubItems.Add(floattostr(i));
end;
ListView1.Items.Item[ListView1.Items.Count-1].MakeVisible(False); //让ListView滚动到最后一项 }
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowList:=sh.create();
end;end.
线程代码如下:
unit Unit2;interfaceuses
Classes,windows,sysutils,graphics;type
sh = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor create(); end;implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure sh.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ sh }constructor sh.create;
begin
inherited create(false);
self.FreeOnTerminate:=true;
end;procedure sh.Execute;
var
i:integer;
begin
for i :=1 to 1500 do
begin
with Form1.ListView1.Items.Add do //显示不
begin
Caption:=inttostr(i); //次序
SubItems.Add(floattostr(i));
SubItems.Add(floattostr(i));
end;
Form1.ListView1.Items.Item[Form1.ListView1.Items.Count-1].MakeVisible(False); //让ListView滚动到最后一项 }
end;
end;end.
控件有:ListView1;Button1,Button2
主程序代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Unit2,
Dialogs, StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
ShowList:sh;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i :=1 to 1000 do
begin
with ListView1.Items.Add do //显示不
begin
Caption:=inttostr(i); //次序
SubItems.Add(floattostr(i));
SubItems.Add(floattostr(i));
end;
ListView1.Items.Item[ListView1.Items.Count-1].MakeVisible(False); //让ListView滚动到最后一项 }
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowList:=sh.create();
end;end.
线程代码如下:
unit Unit2;interfaceuses
Classes,windows,sysutils,graphics;type
sh = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor create(); end;implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure sh.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ sh }constructor sh.create;
begin
inherited create(false);
self.FreeOnTerminate:=true;
end;procedure sh.Execute;
var
i:integer;
begin
for i :=1 to 1500 do
begin
with Form1.ListView1.Items.Add do //显示不
begin
Caption:=inttostr(i); //次序
SubItems.Add(floattostr(i));
SubItems.Add(floattostr(i));
end;
Form1.ListView1.Items.Item[Form1.ListView1.Items.Count-1].MakeVisible(False); //让ListView滚动到最后一项 }
end;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货