W
wubo830814
Unregistered / Unconfirmed
GUEST, unregistred user!
unit ThreadDown;
interface
uses
Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
SysUtils, Dialogs, ComCtrls, Windows, Messages, Graphics, Controls,
Forms, ExtCtrls, StdCtrls;
type
ThDown = class(TThread)
private
procedure IdHTTPWork(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCount: Integer);
procedure IdHTTPWorkbegin
(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCountMax: Integer);
procedure IdHTTPWorkEnd(ASender: TObject;
AWorkMode: TWorkMode);
{ Private declarations }
protected
procedure Execute;
override;
proceduredo
wnLoads;
public
TUrl,TName: String;
TPro: TProgressbar;
TId: TIdHTTP;
constructor Create(FURL, FName: String;
Pro: TProgressbar);
end;
implementation
{ 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 ThDown.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ ThDown }
procedure ThDown.IdHTTPWork(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCount: Integer);
//下载中
begin
TPro.Position:=AWorkCount;
Application.ProcessMessages;
end;
procedure ThDown.IdHTTPWorkbegin
(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCountMax: Integer);
begin
TPro.Max:=AWorkCountMax;
//变化的最大数,也许很大 //下载开始
//TPro.Position:=0;
end;
procedure ThDown.IdHTTPWorkEnd(ASender: TObject;
AWorkMode: TWorkMode);
begin
TPro.Position:=100;
Application.ProcessMessages;
//下载结束
showmessage('下载结束');
end;
constructor ThDown.Create(FUrl, FName: String;
Pro: TProgressbar);
begin
inherited Create(True);
TUrl:=FUrl;
TName:=FName;
TPro:= Pro;
TId:=TIdHTTP.Create(nil);
TId.OnWork:=IdHTTPWork;
TId.OnWorkbegin
:=IdHTTPWorkbegin
;
TId.OnWorkEnd:=IdHTTPWorkend;
FreeOnTerminate:=True;
end;
procedure ThDown.DownLoads;
//下载文件
var
str:TMemoryStream;
begin
str:=TMemoryStream.Create;
try
TId.Get(TUrl,str);
//保存到内存流
str.SaveToFile(TName);
//保存为文件
except end;
str.Free;
end;
procedure ThDown.Execute;
begin
Synchronize(DownLoads);
{ Place thread code here }
end;
end.
//调用线程
procedure TFormMain.LVDownListCustomDrawSubItem(Sender: TCustomListView;
Item: TListItem;
SubItem: Integer;
State: TCustomDrawState;
var DefaultDraw: Boolean);
var
DRect: TRect;
PItemCtrl;
begin
if SubItem in [2] then
begin
if ListItem.Data=Nil then
begin
new(P);
//创建一个指针
DefaultDraw:=False;
//不显示默认的文本.
DRect:= Item.DisplayRect(drBounds);
//获取Item显示的区域.
DRect.Top:=DRect.Top+2;
DRect.Left:= DRect.Right-LVDownList.Columns[2].Width-LVDownList.Columns[3].Width+10;
DRect.Right:= DRect.Left+LVDownList.Columns[2].Width-20;
P.Pro:=TProgressbar.Create(LVDownList);
P.Pro.Parent:=LVDownList;
P.Pro.BoundsRect:= DRect;
P.Th:=ThDown.Create(TUrl,TPath,P.Pro);
P.Th.Resume;
Item.Data:=P;
end;
end;
我想用多线程实现下载,但是现在的问题是,每次点击下载都要等待。
下载一个文件还行,如果多添加几个它就不动了,怎么才能互不干涉同步下载
如果有类似源码,贡献一下,多谢了
interface
uses
Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
SysUtils, Dialogs, ComCtrls, Windows, Messages, Graphics, Controls,
Forms, ExtCtrls, StdCtrls;
type
ThDown = class(TThread)
private
procedure IdHTTPWork(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCount: Integer);
procedure IdHTTPWorkbegin
(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCountMax: Integer);
procedure IdHTTPWorkEnd(ASender: TObject;
AWorkMode: TWorkMode);
{ Private declarations }
protected
procedure Execute;
override;
proceduredo
wnLoads;
public
TUrl,TName: String;
TPro: TProgressbar;
TId: TIdHTTP;
constructor Create(FURL, FName: String;
Pro: TProgressbar);
end;
implementation
{ 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 ThDown.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ ThDown }
procedure ThDown.IdHTTPWork(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCount: Integer);
//下载中
begin
TPro.Position:=AWorkCount;
Application.ProcessMessages;
end;
procedure ThDown.IdHTTPWorkbegin
(ASender: TObject;
AWorkMode: TWorkMode;
AWorkCountMax: Integer);
begin
TPro.Max:=AWorkCountMax;
//变化的最大数,也许很大 //下载开始
//TPro.Position:=0;
end;
procedure ThDown.IdHTTPWorkEnd(ASender: TObject;
AWorkMode: TWorkMode);
begin
TPro.Position:=100;
Application.ProcessMessages;
//下载结束
showmessage('下载结束');
end;
constructor ThDown.Create(FUrl, FName: String;
Pro: TProgressbar);
begin
inherited Create(True);
TUrl:=FUrl;
TName:=FName;
TPro:= Pro;
TId:=TIdHTTP.Create(nil);
TId.OnWork:=IdHTTPWork;
TId.OnWorkbegin
:=IdHTTPWorkbegin
;
TId.OnWorkEnd:=IdHTTPWorkend;
FreeOnTerminate:=True;
end;
procedure ThDown.DownLoads;
//下载文件
var
str:TMemoryStream;
begin
str:=TMemoryStream.Create;
try
TId.Get(TUrl,str);
//保存到内存流
str.SaveToFile(TName);
//保存为文件
except end;
str.Free;
end;
procedure ThDown.Execute;
begin
Synchronize(DownLoads);
{ Place thread code here }
end;
end.
//调用线程
procedure TFormMain.LVDownListCustomDrawSubItem(Sender: TCustomListView;
Item: TListItem;
SubItem: Integer;
State: TCustomDrawState;
var DefaultDraw: Boolean);
var
DRect: TRect;
PItemCtrl;
begin
if SubItem in [2] then
begin
if ListItem.Data=Nil then
begin
new(P);
//创建一个指针
DefaultDraw:=False;
//不显示默认的文本.
DRect:= Item.DisplayRect(drBounds);
//获取Item显示的区域.
DRect.Top:=DRect.Top+2;
DRect.Left:= DRect.Right-LVDownList.Columns[2].Width-LVDownList.Columns[3].Width+10;
DRect.Right:= DRect.Left+LVDownList.Columns[2].Width-20;
P.Pro:=TProgressbar.Create(LVDownList);
P.Pro.Parent:=LVDownList;
P.Pro.BoundsRect:= DRect;
P.Th:=ThDown.Create(TUrl,TPath,P.Pro);
P.Th.Resume;
Item.Data:=P;
end;
end;
我想用多线程实现下载,但是现在的问题是,每次点击下载都要等待。
下载一个文件还行,如果多添加几个它就不动了,怎么才能互不干涉同步下载
如果有类似源码,贡献一下,多谢了