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天
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天
解决方案 »
- 统计listbox1增加了的记录
- 错误信息:Timeout expired
- 这样的SQL语句如何写?
- 初级问题:怎样把 ALT+s 这个组合键传到指定的程序中呢?
- dbgrideh中,字段的displayformat属性能直接将小数转成百分比吗?
- 如何将edit的text写入fastreport
- DELPHI怎么连接MYSQL???
- 还有一个问题,如何将access里的某个表另存为dbf文件,已经没有分了,能帮忙我感激涕零,不理我也是您的本分!
- 转:宝兰,和春天有个约会
- 一个很实际,很有用的问题!!!!!!!!!!!!!!
- 菜鸟问题DMMainDate.sctClient在delphi中编译不了
- delphi怎么导出到ecel2007
idhttp1 := Tidhttp.Create(nil);
while 控制标记 do begin
try
...
finally
//idhttp1.Free;
//Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id);这两句不要
end;
end;
idhttp1.Free;这样才是合理结构
begin
// for i := 1 to 2 do
Windows.CreateThread(nil, 0, @MyProcedure, nil, 0, id)
// MyProcedure();
end;
你看看这里,本来是有2个线程的,一直出错,才注释掉的
感觉这样写线程挺别扭的呢,你用delphi自带的线程类生成工具来生成一个线程类多简单
TThread类?
我觉得不好用。还麻烦。
把 Windows.CreateThread 改成 BeginThread 应该就OK了。具体原因可参考:
http://archive.cnblogs.com/a/2085661/另外你的代码,乱!
在线程函数中再CreateThread,这不是要死循环吗?