16,748
社区成员
发帖
与我相关
我的任务
分享
type
TMyThread = class(TThread)
protected
procedure Execute; override;
end;
procedure TMyThread.Execute;
var
i: Integer;
begin
FreeOnTerminate := True; {这可以让线程执行完毕后随即释放}
for i := 0 to 500000 do
begin
sleep(500);
form1.Memo1.text := form1.idhttp1.get('网站地址');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyThread.Create(False);
end;
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.
procedure Tget.Execute;
var htp: TIdHTTP ;
begin
while not Terminated do
begin
try
if str <> '' then str := '' ;
htp := TIdHTTP.Create(nil) ;
str := htp.Get('http://topic.csdn.net/u/20100409/13/fec3bb35-2dc2-431a-950e-c8f20e8b44c8.html?62489') ;
FreeAndNil(htp);
Synchronize(synWrite);
except
//ShowMessage('出错了..');
if htp <> nil then FreeAndNil(htp);
end;
Sleep(500);
end;
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, idhttp, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tget = class(TThread)
private
str: string ;
count: DWORD ;
protected
procedure Execute ; override ;
public
procedure synWrite ;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ Tget }
procedure Tget.Execute;
var htp: TIdHTTP ;
begin
htp := TIdHTTP.Create(nil) ;
while not Terminated do
begin
try
if str <> '' then str := '' ;
str := htp.Get('http://topic.csdn.net/u/20100409/13/fec3bb35-2dc2-431a-950e-c8f20e8b44c8.html?62489') ;
Synchronize(synWrite);
except
end;
Sleep(500);
end;
htp.Free ;
end;
procedure Tget.synWrite;
begin
Form1.Memo1.Text := str ;
Inc(count) ;
Form1.Caption := IntToStr(count) ; //查看线程是否意外中止.
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Tget.Create(False) ;
end;
end.
type
TMyThread = class(TThread)
protected
procedure Execute; override;
Private
FIDhttp: TIdhttp;
public
Constructor Create(CreateSuspended: BOolean);
Destructor Destroy; override;
end;
// create
begin
inherited create(createsuspended);
FIDhttp := TIDhttp.create(nil);
end;
// destroy
begin
FIdhttp.Free;
end;
procedure TMyThread.Execute;
var
i: Integer;
begin
FreeOnTerminate := True; {这可以让线程执行完毕后随即释放}
for i := 0 to 500000 do
begin
sleep(500);
form1.Memo1.text := form1.idhttp1.get('网站地址');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyThread.Create(False);
end;