如何用多线程写下载文件并显示进度条,请高手看看我哪里不对 ( 积分: 50 )

  • 主题发起人 主题发起人 Jaspers
  • 开始时间 开始时间
J

Jaspers

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UrlMon, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit,
cxMaskEdit, cxButtonEdit, ComCtrls, ExtActns;
type
TForm1 = class(TForm)
edtRemote: TEdit;
Label1: TLabel;
Label2: TLabel;
edtLocal2: TcxButtonEdit;
ProgressBar1: TProgressBar;
Button2: TButton;
Edit1: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TDownloadThread = class(TThread)
private
FProgBar: TProgressBar;
FRemote: string;
FLocal: string;
proceduredo
wnLoadFile;
proceduredo
OnDownLoadProgress(Sender: TDownLoadURL;
Progress, ProgressMax:Cardinal;
StatusCode: TURLDownloadStatus;
StatusText: String;
var Cancel: Boolean);
protected
constructor Create(ABar: TProgressBar;ARemote, ALocal: string);

procedure Execute;
override;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TDownloadThread }
constructor TDownloadThread.Create(ABar: TProgressBar;ARemote, ALocal: string);
begin
FProgBar := ABar;
FRemote := ARemote;
FLocal := ALocal;
inherited Create(true);

end;

procedure TDownloadThread.DoOnDownLoadProgress(Sender: TDownLoadURL;
Progress, ProgressMax: Cardinal;
StatusCode: TURLDownloadStatus;
StatusText: String;
var Cancel: Boolean);
begin

FProgBar.Position := Progress;
FProgBar.Max := ProgressMax;
end;

procedure TDownloadThread.DownLoadFile;
begin
with TDownloadURL.Create(nil)do
try
URL := FRemote;
FileName := FLocal;
OnDownloadProgress :=do
OnDownLoadProgress;
ExecuteTarget(nil);
finally
Free;
end;
end;

procedure TDownloadThread.Execute;
begin
inherited;
Synchronize(DownloadFile);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
thrd: TDownloadThread;
begin
thrd := TDownloadThread.Create(ProgressBar1, edtRemote.Text, edtLocal.Text);
thrd.Resume;
end;

end.
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UrlMon, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit,
cxMaskEdit, cxButtonEdit, ComCtrls, ExtActns;
type
TForm1 = class(TForm)
edtRemote: TEdit;
Label1: TLabel;
Label2: TLabel;
edtLocal2: TcxButtonEdit;
ProgressBar1: TProgressBar;
Button2: TButton;
Edit1: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TDownloadThread = class(TThread)
private
FProgBar: TProgressBar;
FRemote: string;
FLocal: string;
proceduredo
wnLoadFile;
proceduredo
OnDownLoadProgress(Sender: TDownLoadURL;
Progress, ProgressMax:Cardinal;
StatusCode: TURLDownloadStatus;
StatusText: String;
var Cancel: Boolean);
protected
constructor Create(ABar: TProgressBar;ARemote, ALocal: string);

procedure Execute;
override;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TDownloadThread }
constructor TDownloadThread.Create(ABar: TProgressBar;ARemote, ALocal: string);
begin
FProgBar := ABar;
FRemote := ARemote;
FLocal := ALocal;
inherited Create(true);

end;

procedure TDownloadThread.DoOnDownLoadProgress(Sender: TDownLoadURL;
Progress, ProgressMax: Cardinal;
StatusCode: TURLDownloadStatus;
StatusText: String;
var Cancel: Boolean);
begin

FProgBar.Position := Progress;
FProgBar.Max := ProgressMax;
end;

procedure TDownloadThread.DownLoadFile;
begin
with TDownloadURL.Create(nil)do
try
URL := FRemote;
FileName := FLocal;
OnDownloadProgress :=do
OnDownLoadProgress;
ExecuteTarget(nil);
finally
Free;
end;
end;

procedure TDownloadThread.Execute;
begin
inherited;
Synchronize(DownloadFile);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
thrd: TDownloadThread;
begin
thrd := TDownloadThread.Create(ProgressBar1, edtRemote.Text, edtLocal.Text);
thrd.Resume;
end;

end.
 
我也想知道。。帮你
 
有什么问题啊?
不过怎么看都觉得是单线程……
特别是
Synchronize(DownloadFile);
 
Mr/Msdo
rice
你说的对! 下载的时候主界面不能动。改如何改呢?
 
应该用Synchronize执行更新VCL界面之类的操作
Synchronize表示函数将在主线程中执行,你整个DownloadFile都放在这里,那不是一直只有单个线程?
你把更新界面的操作单独出来,放在Synchronize里执行,其他的就直接放在Execute吧
 
可以加我一起讨论嘛
53252734
 
Dorice同志:
問題在於更新界面的do
OnDownLoadProgress 是一個event handler, 並不是我自己call的, 慘!
aries_jo同志:
很抱歉啊,我沒有用qq的習慣...
 
后退
顶部