Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止idhttp.get时的析构函数(indy,delphi)_Delphi_Destructor_Terminate_Disconnect_Idhttp - Fatal编程技术网

停止idhttp.get时的析构函数(indy,delphi)

停止idhttp.get时的析构函数(indy,delphi),delphi,destructor,terminate,disconnect,idhttp,Delphi,Destructor,Terminate,Disconnect,Idhttp,我的应用程序可以从memo1中的每个url下载一张图片。 它使用idhttp.get并有一个skipbutton。跳过后,下载下一张图片 Q1:您是否有代码放入析构函数,以及“终止”和“等待”的代码是什么? 我在另一个网站上发现: destructor thread.destroy; begin try Terminate; If HTTP.Connected then HTTP.Disconnect; finally WaitFor; FreeAndNil(HTTP); end; inheri

我的应用程序可以从memo1中的每个url下载一张图片。 它使用idhttp.get并有一个skipbutton。跳过后,下载下一张图片

Q1:您是否有代码放入析构函数,以及“终止”和“等待”的代码是什么? 我在另一个网站上发现:

destructor thread.destroy;
begin
try
Terminate;
If HTTP.Connected then HTTP.Disconnect;
finally
WaitFor;
FreeAndNil(HTTP);
end;
inherited;
end;
Q2:如何调用析构函数并使其工作?

Q3:您有提示(特别是安全问题)和其他代码行吗?

我的申请代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;

type
       thread = class
  public
      Constructor Create; overload;
      Destructor  Destroy; override;
  end;

  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    Memo1: TMemo;
    Memo2: TMemo;
    Memo3: TMemo;
    startbutton: TButton;
    skipbutton: TButton;

    procedure startbuttonClick(Sender: TObject);
    procedure skipbuttonClick(Sender: TObject);
    procedure IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);

         end;
var
  Form1: TForm1;
     http: tidhttp;
     s: boolean;
implementation

{$R *.dfm}

            constructor thread.Create;
begin
      HTTP := TIdHTTP.Create(nil);
      inherited ;
end;

           destructor thread.destroy;
begin
try

If HTTP.Connected then HTTP.Disconnect;
finally
FreeAndNil(HTTP);
end;
inherited;
end;


procedure TForm1.startbuttonClick(Sender: TObject);
var
i: integer;
  fs : TFileStream ;
begin
for i:= 0 to memo1.lines.count-1 do begin
s:= false;
   fs := TFileStream.Create(inttostr(i)+'abc.jpg', fmCreate);
   http:= idhttp1;
   try
   try
HTTP.Get(memo1.lines[i],fs);
memo2.Lines.add(memo1.Lines[i]);
except
on E: Exception do
begin
memo3.lines.add(' ha ha ha not working   '+syserrormessage(getlasterror));
end;
end;
finally
fs.free;
end;
end;

  end;

procedure TForm1.skipbuttonClick(Sender: TObject);
    begin
s:=true;
end;

procedure TForm1.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
application.ProcessMessages;

     if s = true then
http.Disconnect;

end;

end.

由于您正在从GUI(=主线程)使用
IdHttp
,并且
Indy
正在阻塞,因此有两个选项:a)将
IdAntifreeze
与消息结合使用(只需将组件放到表单上),b)使用线程

不要使用
应用程序。处理消息
,因为这会导致奇怪的副作用

现在回答您的问题:

Q1:您在internet上找到的代码实现了解决方案b),因此这不适用于您当前的代码

Q2:与Q1相同

Q3:这是一个正确实现解决方案a的版本)

这段代码仍然不是100%完美,因为它没有实现禁用/启用starttransfer和skiptransfer按钮的逻辑(我把它留给您作为练习:)

单元16;
接口
使用
窗口、消息、系统工具、变体、类、图形、控件、窗体、,
对话框、StdCtrls、IdAntiFreeze、IdAntiFreeze、IdBaseComponent、IdComponent、IdTCPConnection、IdTCPClient、IdHTTP;
常数
WM_传输=WM_用户+1;
类型
TForm1=类(TForm)
IdHTTP1:TIdHTTP;
I防冻剂1:TID防冻剂;
备忘录1:TMemo;
Btn_启动:t按钮;
Btn_跳过:t按钮;
备忘录2:TMemo;
程序IDHTTP1工作(ASender:ToObject;AWorkMode:TWorkMode;AWorkCount:Int64);
程序Btn_startClick(发送方:TObject);
程序Btn_skipClick(发送方:TObject);
私有的
{私有声明}
转移:布尔;
url索引:整数;
程序下一次传输(var msg:TMessage);信息WM_传输;
程序启动转移;
转移程序;
程序技能转移;
结束;
变量
表1:TForm1;
实施
{$R*.dfm}
程序TForm1.NextTransfer(var msg:TMessage);
开始
转移;
结束;
程序TForm1.SkipTransfer;
开始
转移:=假;
结束;
程序TForm1.StartTransfer;
开始
URL索引:=0;
转移;
结束;
程序TForm1.DoTransfer;
变量
Url:String;
溪流:TStringStream ;;
开始
如果urleindex
DFM文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 290
  ClientWidth = 707
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 92
    Top = 12
    Width = 213
    Height = 257
    Lines.Strings = (
      'http://stackoverflow.com'
      'http://www.google.com'
      'http://www.hardocp.com'
      '')
    TabOrder = 0
    WordWrap = False
  end
  object Btn_start: TButton
    Left = 8
    Top = 128
    Width = 75
    Height = 25
    Caption = 'Btn_start'
    TabOrder = 1
    OnClick = Btn_startClick
  end
  object Btn_skip: TButton
    Left = 8
    Top = 159
    Width = 75
    Height = 25
    Caption = 'Btn_skip'
    TabOrder = 2
    OnClick = Btn_skipClick
  end
  object Memo2: TMemo
    Left = 320
    Top = 12
    Width = 373
    Height = 257
    TabOrder = 3
    WordWrap = False
  end
  object IdHTTP1: TIdHTTP
    OnWork = IdHTTP1Work
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.ContentRangeEnd = -1
    Request.ContentRangeStart = -1
    Request.ContentRangeInstanceLength = -1
    Request.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    Request.BasicAuthentication = False
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    Request.Ranges.Units = 'bytes'
    Request.Ranges = <>
    HTTPOptions = [hoForceEncodeParams]
    Left = 24
    Top = 16
  end
  object IdAntiFreeze1: TIdAntiFreeze
    Left = 16
    Top = 72
  end
end
对象格式1:t格式1
左=0
Top=0
标题='Form1'
ClientHeight=290
ClientWidth=707
颜色=clBtnFace
Font.Charset=默认字符集
Font.Color=clWindowText
字体高度=-11
Font.Name='Tahoma'
Font.Style=[]
OldCreateOrder=False
PixelsPerInch=96
text高度=13
对象备忘录1:TMemo
左=92
Top=12
宽度=213
高度=257
行。字符串=(
'http://stackoverflow.com'
'http://www.google.com'
'http://www.hardocp.com'
'')
TabOrder=0
WordWrap=False
结束
对象Btn_开始:t按钮
左=8
Top=128
宽度=75
高度=25
标题='Btn\U start'
TabOrder=1
OnClick=Btn\u开始单击
结束
对象Btn_跳过:t按钮
左=8
Top=159
宽度=75
高度=25
标题='Btn\U skip'
TabOrder=2
OnClick=Btn\u skipClick
结束
对象2:TMemo
左=320
Top=12
宽度=373
高度=257
TabOrder=3
WordWrap=False
结束
对象IdHTTP1:TIdHTTP
OnWork=IdHTTP1Work
AllowCookies=True
ProxyParams.BasicAuthentication=False
ProxyParams.ProxyPort=0
Request.ContentLength=-1
Request.ContentRangeEnd=-1
Request.ContentRangeStart=-1
Request.ContentRangeInstanceLength=-1
Accept='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
Request.BasicAuthentication=False
Request.UserAgent='Mozilla/3.0(兼容;Indy库)'
Request.Ranges.Units='bytes'
请求。范围=
HTTPOptions=[hoForceEncodeParams]
左=24
Top=16
结束
对象ID防冻剂1:TID防冻剂
左=16
顶部=72
结束
结束

由于您正在从GUI(=主线程)使用
IdHttp
,并且
Indy
正在阻塞,因此有两个选项:a)将
IdAntifreeze
与消息结合使用(只需将组件放到表单上),b)使用线程

不要使用
应用程序。处理消息
,因为这会导致奇怪的副作用

现在回答您的问题:

Q1:您在internet上找到的代码实现了解决方案b),因此这不适用于您的当前版本
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 290
  ClientWidth = 707
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 92
    Top = 12
    Width = 213
    Height = 257
    Lines.Strings = (
      'http://stackoverflow.com'
      'http://www.google.com'
      'http://www.hardocp.com'
      '')
    TabOrder = 0
    WordWrap = False
  end
  object Btn_start: TButton
    Left = 8
    Top = 128
    Width = 75
    Height = 25
    Caption = 'Btn_start'
    TabOrder = 1
    OnClick = Btn_startClick
  end
  object Btn_skip: TButton
    Left = 8
    Top = 159
    Width = 75
    Height = 25
    Caption = 'Btn_skip'
    TabOrder = 2
    OnClick = Btn_skipClick
  end
  object Memo2: TMemo
    Left = 320
    Top = 12
    Width = 373
    Height = 257
    TabOrder = 3
    WordWrap = False
  end
  object IdHTTP1: TIdHTTP
    OnWork = IdHTTP1Work
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.ContentRangeEnd = -1
    Request.ContentRangeStart = -1
    Request.ContentRangeInstanceLength = -1
    Request.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    Request.BasicAuthentication = False
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    Request.Ranges.Units = 'bytes'
    Request.Ranges = <>
    HTTPOptions = [hoForceEncodeParams]
    Left = 24
    Top = 16
  end
  object IdAntiFreeze1: TIdAntiFreeze
    Left = 16
    Top = 72
  end
end