中断UrlDownloadToFile(100分)

  • 主题发起人 主题发起人 chen_ht
  • 开始时间 开始时间
C

chen_ht

Unregistered / Unconfirmed
GUEST, unregistred user!
用UrlDownloadToFile下载一文件(80k),用20分钟还没有完成,
如何中断该过程去执行下一条指令。
 
可以加入下载进度的callback proc的!
 
在线程中来做,强制终止线程即可
 
//-----------------------------------------------------------------------------
function TfrmUpdate.OnProgress(ulProgress, ulProgressMax,ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;
var
Status:string;
begin
case ulStatusCode of
BINDSTATUS_FINDINGRESOURCE : Status:='正在查找站点:' + szStatusText;
BINDSTATUS_CONNECTING : Status:='正在连接站点:' + szStatusText;
BINDSTATUS_REDIRECTING : Status:='重新定位...';
BINDSTATUS_BEGINDOWNLOADDATA : Status:='开始下载:' + szStatusText;
BINDSTATUS_DOWNLOADINGDATA : Status:='正在下载...';
BINDSTATUS_ENDDOWNLOADDATA : Status:='下载完毕:' + szStatusText;
BINDSTATUS_BEGINDOWNLOADCOMPONENTS : Status:='开始下载信息';
BINDSTATUS_INSTALLINGCOMPONENTS : Status:='开始安装信息...' ;
BINDSTATUS_ENDDOWNLOADCOMPONENTS : Status:='结束下载信息';
BINDSTATUS_USINGCACHEDCOPY : Status:='正在从缓冲区拷贝...';
BINDSTATUS_SENDINGREQUEST : Status:='正在发送请求...';
BINDSTATUS_CLASSIDAVAILABLE : Status:='CLSID 资源有效';
BINDSTATUS_MIMETYPEAVAILABLE : Status:='MIME 资源有效';
BINDSTATUS_CACHEFILENAMEAVAILABLE : Status:='缓冲区文件名可用';
BINDSTATUS_BEGINSYNCOPERATION : Status:='开始同步操作';
BINDSTATUS_ENDSYNCOPERATION : Status:='停止同步操作';
BINDSTATUS_BEGINUPLOADDATA : Status:='开始上传信息';
BINDSTATUS_UPLOADINGDATA : Status:='正在上传信息';
BINDSTATUS_ENDUPLOADDATA : Status:='停止上传信息';
BINDSTATUS_PROTOCOLCLASSID : Status:='协议可用';
BINDSTATUS_ENCODING : Status:='正在编码...';
BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : Status:='MIME检验通过';
BINDSTATUS_CLASSINSTALLLOCATION : Status:='CLASSINSTALLLOCATION';
BINDSTATUS_DECODING : Status:='正在解码...';
BINDSTATUS_LOADINGMIMEHANDLER : Status:='LOADINGMIMEHANDLER';
BINDSTATUS_CONTENTDISPOSITIONATTACH : Status:='CONTENTDISPOSITIONATTACH';
BINDSTATUS_FILTERREPORTMIMETYPE : Status:='FILTERREPORTMIMETYPE';
BINDSTATUS_CLSIDCANINSTANTIATE : Status:='CLSIDCANINSTANTIATE';
BINDSTATUS_IUNKNOWNAVAILABLE : Status:='IUNKNOWNAVAILABLE';
BINDSTATUS_DIRECTBIND : Status:='DIRECTBIND';
BINDSTATUS_RAWMIMETYPE : Status:='MIME type of the resource, before any code sniffing is done';
BINDSTATUS_PROXYDETECTING : Status:='监测代理服务器...';
BINDSTATUS_ACCEPTRANGES : Status:='Valid types of range requests for a resource';
end;

Application.ProcessMessages;

//这个地方是你想要的!

if DoCancel then
result := E_ABORT
else
result := S_OK;

plStatus.Caption:=Status;
plProgress.Caption:='已下载:' + IntToStr(ulProgress) +
';总共:' + IntToStr(ulProgressMax);
end;
 
这个进度怎么用啊,谁来调用他啊
 
type
TfrmUpdate = class(TForm,IBindStatusCallback)
ImgTop: TImage;
LblTop: TLabel;
SpdBtnClose: TSpeedButton;
Bevel1: TBevel;
plStatus: TPanel;
plInfo: TPanel;
btnCancel: TFlatSpeedButton;
tmrUdapte: TTimer;
plProgress: TPanel;
procedure tmrUdapteTimer(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure ImgTopMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure SpdBtnCloseClick(Sender: TObject);
private
function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
function GetPriority(out nPriority): HResult; stdcall;
function OnLowResource(reserved: DWORD): HResult; stdcall;
function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;szStatusText: LPCWSTR): HResult; stdcall;
function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;
function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;
function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;stgmed: PStgMedium): HResult; stdcall;
function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;
function GetHTMLFile(URL, FileName: string): HRESULT;
public
{ Public declarations }
end;
 
to 爱元元的哥哥:如何调用?另外 BINDSTATUS_CONTENTDISPOSITIONATTACH:Status:='CONTENTDISPOSITIONATTACH';
处开始出错。
 
const
BINDSTATUS_CONTENTDISPOSITIONATTACH = BINDSTATUS_LOADINGMIMEHANDLER + 1;
BINDSTATUS_FILTERREPORTMIMETYPE = BINDSTATUS_CONTENTDISPOSITIONATTACH + 1;
BINDSTATUS_CLSIDCANINSTANTIATE = BINDSTATUS_FILTERREPORTMIMETYPE + 1;
BINDSTATUS_IUNKNOWNAVAILABLE = BINDSTATUS_CLSIDCANINSTANTIATE + 1;
BINDSTATUS_DIRECTBIND = BINDSTATUS_IUNKNOWNAVAILABLE + 1;
BINDSTATUS_RAWMIMETYPE = BINDSTATUS_DIRECTBIND + 1;
BINDSTATUS_PROXYDETECTING = BINDSTATUS_RAWMIMETYPE + 1;
BINDSTATUS_ACCEPTRANGES = BINDSTATUS_PROXYDETECTING + 1;

到msdn里搜一下,这种代码到处都是
 
在UrlDownloadToFile(nil, PChar(s), PChar(s3), 0,nil );如何调用OnProgress,是在后一个nil吗? DoCancel then出错。我的delphi水平还不能读懂上面的东西。
 
根据爱元元的哥哥的其他问题整理

unit UfrmUpdate;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,UrlMon, ActiveX, StdCtrls;

type
TfrmUpdate = class(TForm,IBindStatusCallback)
plStatus: TLabel;
plProgress: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
function GetPriority(out nPriority): HResult; stdcall;
function OnLowResource(reserved: DWORD): HResult; stdcall;
function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;szStatusText: LPCWSTR): HResult; stdcall;
function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;
function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;
function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;stgmed: PStgMedium): HResult; stdcall;
function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;
function GetHTMLFile(URL, FileName: string): HRESULT;

public
{ Public declarations }
end;

var
frmUpdate: TfrmUpdate;
DoCancel:bool;

implementation

{$R *.dfm}
function TfrmUpdate.GetHTMLFile( URL , FileName : string) : HRESULT;
var
status : IBindStatusCallback ;
begin
status := IBindStatusCallback(self);
result := UrlDownLoadToFile(nil, pChar( URL ) ,pChar( FileName ),0 ,Status );
end;

//------------------------------------------------------------------------------
function TfrmUpdate.GetBindInfo(out grfBINDF: DWORD;
var bindinfo: TBindInfo): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.GetPriority(out nPriority): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.OnDataAvailable(grfBSCF, dwSize: DWORD;
formatetc: PFormatEtc; stgmed: PStgMedium): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.OnLowResource(reserved: DWORD): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.OnObjectAvailable(const iid: TGUID;
punk: IInterface): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.OnStartBinding(dwReserved: DWORD;
pib: IBinding): HResult;
begin
result := E_NOTIMPL;
end;

//------------------------------------------------------------------------------
function TfrmUpdate.OnStopBinding(hresult: HResult;
szError: LPCWSTR): HResult;
begin
result := E_NOTIMPL;
end;

//-----------------------------------------------------------------------------
function TfrmUpdate.OnProgress(ulProgress, ulProgressMax,ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;
var
Status:string;
begin
case ulStatusCode of
BINDSTATUS_FINDINGRESOURCE : Status:='正在查找站点:' + szStatusText;
BINDSTATUS_CONNECTING : Status:='正在连接站点:' + szStatusText;
BINDSTATUS_REDIRECTING : Status:='重新定位...';
BINDSTATUS_BEGINDOWNLOADDATA : Status:='开始下载:' + szStatusText;
BINDSTATUS_DOWNLOADINGDATA : Status:='正在下载...';
BINDSTATUS_ENDDOWNLOADDATA : Status:='下载完毕:' + szStatusText;
BINDSTATUS_BEGINDOWNLOADCOMPONENTS : Status:='开始下载信息';
BINDSTATUS_INSTALLINGCOMPONENTS : Status:='开始安装信息...' ;
BINDSTATUS_ENDDOWNLOADCOMPONENTS : Status:='结束下载信息';
BINDSTATUS_USINGCACHEDCOPY : Status:='正在从缓冲区拷贝...';
BINDSTATUS_SENDINGREQUEST : Status:='正在发送请求...';
BINDSTATUS_CLASSIDAVAILABLE : Status:='CLSID 资源有效';
BINDSTATUS_MIMETYPEAVAILABLE : Status:='MIME 资源有效';
BINDSTATUS_CACHEFILENAMEAVAILABLE : Status:='缓冲区文件名可用';
BINDSTATUS_BEGINSYNCOPERATION : Status:='开始同步操作';
BINDSTATUS_ENDSYNCOPERATION : Status:='停止同步操作';
BINDSTATUS_BEGINUPLOADDATA : Status:='开始上传信息';
BINDSTATUS_UPLOADINGDATA : Status:='正在上传信息';
BINDSTATUS_ENDUPLOADDATA : Status:='停止上传信息';
BINDSTATUS_PROTOCOLCLASSID : Status:='协议可用';
BINDSTATUS_ENCODING : Status:='正在编码...';
BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : Status:='MIME检验通过';
BINDSTATUS_CLASSINSTALLLOCATION : Status:='CLASSINSTALLLOCATION';
BINDSTATUS_DECODING : Status:='正在解码...';
BINDSTATUS_LOADINGMIMEHANDLER : Status:='LOADINGMIMEHANDLER';
BINDSTATUS_CONTENTDISPOSITIONATTACH : Status:='CONTENTDISPOSITIONATTACH';
BINDSTATUS_FILTERREPORTMIMETYPE : Status:='FILTERREPORTMIMETYPE';
BINDSTATUS_CLSIDCANINSTANTIATE : Status:='CLSIDCANINSTANTIATE';
BINDSTATUS_IUNKNOWNAVAILABLE : Status:='IUNKNOWNAVAILABLE';
BINDSTATUS_DIRECTBIND : Status:='DIRECTBIND';
BINDSTATUS_RAWMIMETYPE : Status:='MIME type of the resource, before any code sniffing is done';
BINDSTATUS_PROXYDETECTING : Status:='监测代理服务器...';
BINDSTATUS_ACCEPTRANGES : Status:='Valid types of range requests for a resource';
end;

Application.ProcessMessages;

//这个地方是你想要的!

if DoCancel then
result := E_ABORT
else
result := S_OK;

plStatus.Caption:=Status;
plProgress.Caption:='已下载:' + IntToStr(ulProgress) + '字节'+
';总共:' + IntToStr(ulProgressMax)+'字节';
end;


procedure TfrmUpdate.Button1Click(Sender: TObject);
begin
getHtmlFile('http://www.liheng.net/yasuo/ivu.exe','d:/ivu.exe');
end;

procedure TfrmUpdate.Button2Click(Sender: TObject);
begin
DoCancel:=true;
close;
end;

end.

 
能否将一个完整的实例发给我!
我的邮箱:webmaster@51kk.com,谢谢。
 
多人接受答案了。
 
后退
顶部