我现在正在做一个项目,需要一边下载网上图片,一边浏览已下载的图片,浏览图片我用TDirectoryListBox和Timage等控件,下载我用THttpCli控件,且使用多线程(部分代码如下),下载的速度很快,但是同时浏览图片时,感觉反映速度慢.体现在:最大最小化窗口时有停顿现象,拖动Tscrollbar反应慢. 我的意思是:能让下载在后台进行,前台能流畅的操作VCL.谁能帮我指一下方向,先谢了.for i := Low(ThreadsObjects) to High(ThreadsObjects) do //使用最大6个线程
begin
//线程还没被建立
if ThreadsState[i] = tsInexistant then
CreateThread(Self); //线程准备就绪
if ThreadsState[i] = tsReady then
begin
with ThreadsObjects[i] do
begin
FURL := UrlEdit.Text;
FProxy := ProxyEdit.Text;
inc(FileNameInt);
FRcvdStream := TFileStream.Create(ReturnPath(FURL),fmCreate);
SetThreadState(i, tsInUse); // In use
Resume; //get the page
end;
end;
end;
begin
//线程还没被建立
if ThreadsState[i] = tsInexistant then
CreateThread(Self); //线程准备就绪
if ThreadsState[i] = tsReady then
begin
with ThreadsObjects[i] do
begin
FURL := UrlEdit.Text;
FProxy := ProxyEdit.Text;
inc(FileNameInt);
FRcvdStream := TFileStream.Create(ReturnPath(FURL),fmCreate);
SetThreadState(i, tsInUse); // In use
Resume; //get the page
end;
end;
end;
你在線程中操作VCL要用Synchronize來調用你的顯示函數,
另在下載的線程中采用分段處理,每個分段完後調用一下sleep一下,暫停一下線程,前台就會流暢些
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;type
TBounceThread=class(TThread)
private
FShape:TShape;
FXSpeed:Integer;
FYSpeed:integer;
procedure MoveShape;
protected
procedure Execute;override;
public
constructor Create(Suspended:Boolean;Shape:TShape;XSpeed,YSpeed:integer);
property Shape:TShape read fShape;
end; TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}constructor TBounceThread.Create(Suspended: Boolean; Shape: TShape;
XSpeed, YSpeed: integer);
begin
inherited create(Suspended);
fShape:=Shape;
FXSpeed:=XSpeed;
FYSpeed:=YSpeed;
FreeOnTerminate:=true;
end;procedure TBounceThread.Execute;
begin
inherited;
while not Terminated do begin
Synchronize(MoveShape);
//sleep(5);
end;
end;procedure TBounceThread.MoveShape;
var
MaxHeight,MaxWidth:integer;
begin
with fShape do begin
if Parent is TForm then begin
MaxWidth:=TForm(Parent).ClientWidth;
MaxHeight:=TForm(Parent).ClientHeight;
end else begin
MaxWidth:=Parent.Width;
MaxHeight:=Parent.Height;
end;
left:=Left+FXSpeed;
top:=top+FYSpeed;
if (left<0) or (left+Width>MaxWidth) then begin
if left<0 then left:=0;
if left+Width>MaxWidth then left:=MaxWidth-Width;
FXSpeed:=FXSpeed*-1;
end;
if (Top<0) or (Top+Height>MaxHeight) then begin
if top<0 then top:=0;
if top+Height>MaxHeight then top:=MaxHeight-Height;
FYSpeed:=FYSpeed*-1;
end;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
TBounceThread.Create(false,shape1,1,1);
TBounceThread.Create(false,shape2,1,2);
TBounceThread.Create(false,shape3,2,1);
TBounceThread.Create(false,shape4,2,2);
end;end.
经过调试我发现操作VCL的不流畅有一个主要原因: 当下载目录中的文件个数太多时(比如:1000以上), 反应速度就降下, 不知各位有没有碰到过相似的问题. 同理Tlistbox中的items.count太大是否也会?(比如大于10000个).当然和后台运行的线程也有关系, rodyhd(帝客)贴的代码我想运行一下,可会出错.
我在你的代码中加入 var ThreadsObjects:TBounceThread; 然后ThreadsObjects.resume;
不知哪里出错, 请指点.