很简单的访问百度,为什么运行出错?
- Delphi(Pascal) code
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天
[解决办法]
线程没有释放吧?
感觉这样写线程挺别扭的呢,你用delphi自带的线程类生成工具来生成一个线程类多简单