delphi使用Idhttp.get('') 造成假死(堵塞),请问线程idhttp怎么才能做到不出错?


本文整理自网络,侵删。

 
unit Unit1;

interface

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

const
  WM_WebUpdate = WM_USER + 1000;
type
  TForm1 = class(TForm)
    btn1: TButton;
    mmo1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure WMWebUpdate(var AMessage: TMessage); message WM_WebUpdate;
  end;

  TMyThread = class(TThread)
  private
    FRunType: Integer;
    procedure Procedure0;
    procedure Procedure1;
  protected
    procedure Execute; override;
  public
    property RunType: Integer read FRunType write FRunType;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyThread }

procedure TMyThread.Execute;
begin
  inherited;
  case FRunType of
    0: Procedure0;
    1: Procedure1;
  end;
end;

procedure TMyThread.Procedure0;
var
  IDHttp: TIdHTTP;
  sWeb: string;
begin
  IDHttp := TIdHTTP.Create(nil);
  try
    sWeb := IDHttp.Get('http://topic.csdn.net/u/20100409/13/fec3bb35-2dc2-431a-950e-c8f20e8b44c8.html?6345');
    SendMessage(Form1.Handle, WM_WebUpdate, Integer(PChar(sWeb)), 0);
  finally
    IDHttp.Free;
  end;
end;

procedure TMyThread.Procedure1;
begin
  //类别1执行代码
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  MyThread0, MyThread1: TMyThread;
begin
  MyThread0 := TMyThread.Create(True);
  MyThread0.FreeOnTerminate := True;
  MyThread0.RunType := 0;
  MyThread0.Resume;

  MyThread1 := TMyThread.Create(True);
  MyThread1.FreeOnTerminate := True;
  MyThread1.RunType := 1;
  MyThread1.Resume;
end;

procedure TForm1.WMWebUpdate(var AMessage: TMessage);
var
  pWeb: PChar;
begin
  pWeb := PChar(AMessage.WParam);
  mmo1.Lines.Add(pWeb);
end;

end.

相关阅读 >>

Delphi 双击richedit高亮所有关键字

Delphi hook 指定程序窗体和控件的 wndproc

Delphi api: setwindowpos改变窗口的位置与状态

Delphi 2009 中的泛型

Delphi字符串深入学习

Delphi dcc64.exe Delphi64位命令行编译器揭秘

Delphi 检测鼠标键盘多久没有活动

Delphi dbexpress的upwherekeyonly的使用注意事项

Delphi xe5电脑录像功能

Delphi 屏幕取色

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...