unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  StdCtrls, ExtCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  OldProc: Pointer;
  ThreadCount: integer;
  id: Thandle;
  Suc: integer;implementation{$R *.dfm}procedure MyProcedure();
var
  idhttp1: Tidhttp;
  s: string;
  username, pass, email: string;
  Params: TStringStream;
  tmp: string;
begin  try
    InterlockedIncrement(ThreadCount);
    idhttp1 := Tidhttp.Create(nil);
    idhttp1.Request.UserAgent :=
      'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';
    s := idhttp1.get('http://www.baidu.com');
    tmp := '百度';
    if pos(tmp, s) <> 0 then
    begin
      Windows.SendMessage(Form1.Handle, Messages.WM_USER + 100,
        integer(PChar(tmp)), 0);
    end;
    InterlockedDecrement(ThreadCount);
    InterlockedIncrement(Suc);
  finally
    idhttp1.Free;
    Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id);
  end;end;procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  // for i := 1 to 2 do
  Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id)
  // MyProcedure();
end;function NewProc(hwnd, msg, wParam, lParam: longint): LRESULT; stdcall;
begin
  case msg of
    Messages.WM_USER + 100:
      begin
        Form1.Label1.caption := PChar(wParam);
        Form1.Label2.caption := IntToStr(Suc);
      end;
  end;
  Result := Windows.CallWindowProc(OldProc, hwnd, msg, wParam, lParam);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  OldProc := Pointer(Windows.setwindowlong(self.Handle, Windows.GWL_WNDPROC,
    longint(@NewProc)));end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label3.caption := IntToStr(ThreadCount);
end;end.正常运行中,点击叉关闭时,会出现应用程序错误。运行大概几分钟,也会出现应用程序错误。哪位大侠帮我看一下是怎么回事。小弟刚学3天

解决方案 »

  1.   

    仅仅访问页面的文本,喜欢用WinInet函数来完成,
      idhttp1 := Tidhttp.Create(nil);
      while 控制标记 do begin
        try
          ...
        finally
          //idhttp1.Free;
          //Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id);这两句不要
        end;
      end; 
      idhttp1.Free;这样才是合理结构
      

  2.   

    我要用多线程啊,
    begin
      // for i := 1 to 2 do
      Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id)
      // MyProcedure();
    end;
    你看看这里,本来是有2个线程的,一直出错,才注释掉的
      

  3.   

    线程没有释放吧?
    感觉这样写线程挺别扭的呢,你用delphi自带的线程类生成工具来生成一个线程类多简单
      

  4.   


    TThread类?
    我觉得不好用。还麻烦。
      

  5.   

    这个是CreateThread的问题,
    把 Windows.CreateThread 改成 BeginThread 应该就OK了。具体原因可参考:
    http://archive.cnblogs.com/a/2085661/另外你的代码,乱!
    在线程函数中再CreateThread,这不是要死循环吗?