5,929
社区成员




unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Spin, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP, IdAntiFreezeBase, IdAntiFreeze;
type
TWeiBoThread=class(TThread)
private
// IdHttp_Sina:TIdHTTP;
fs:TStringStream;
UpdateFuncID:integer;
IsFinished:Bool;
// FURL:string ;
public
sURL,sFileSave:string;
ThreadSumCount:integer;
ThreadIndex:integer; //当前线程编号
WhatLV:TListView;
// IsBusy:Bool; //是否正在忙
//
// // IsPause_WaitDial:Bool; //主线程是否发货了暂停的命令,如果是,要挂起线程
//
// RoundIndex:integer;
LVIndex, WebIndex:integer;
constructor Create( CreateSuspended: Boolean);
destructor Destroy; override;
procedure Execute ; override;
procedure SendID(nID:string);
procedure ThreadDone( );
procedure UpdateListView();
end;
type
TfmMain = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
edtThreadCount: TSpinEdit;
Label2: TLabel;
btnInit: TButton;
Button1: TButton;
IdHTTP1: TIdHTTP;
IdAntiFreeze1: TIdAntiFreeze;
Button2: TButton;
Button3: TButton;
Button4: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
lvUserList: TListView;
mmLog: TMemo;
procedure btnInitClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
TaskSumNum,FinishedTask:Integer;
end;
var
fmMain: TfmMain;
MyThreadList: array of TWeiBoThread;
implementation
{$R *.dfm}
{ TWeiBoThread }
constructor TWeiBoThread.Create (CreateSuspended: Boolean);
begin
inherited Create(false);
LVIndex:=0;
end;
destructor TWeiBoThread.Destroy;
begin
Terminate; //TY
inherited;
end;
procedure TWeiBoThread.Execute;
var
idHttp_Sina: TIdHttp;
myStream:TStringStream;
begin
//用此句在运行过程中就报错,未知原因。2011-8-16 //●●●问题点4
//FreeOnTerminate := True; {这可以让线程执行完毕后随即释放}
if IsFinished then Exit;
myStream:= TStringStream.Create;
UpdateFuncID:=1; //1表示显示开始时间
Synchronize( UpdateListView );
//WhatLV.Items[LVIndex].SubItems[1]:= '开始:' + FormatDateTime('hh:mm:ss',now ) ;
idHttp_Sina:= TIdHttp.Create(nil);
idHttp_Sina.Get('http://www.163.com?' + IntToStr(LVIndex) , myStream );
//idHttp_Sina.Get('http://www.baidu.com?' + IntToStr(LVIndex) , myStream );
//华军软件园,不同的网页
// idHttp_Sina.Get('http://www.onlinedown.net/soft/' + IntToStr( 44800 + LVIndex) + '.htm' , myStream );
UpdateFuncID:=2; //1表示显示开始时间
Synchronize( UpdateListView );
myStream.SaveToFile( 'OutFile\' +
IntToStr(LVIndex) + '@' + FormatDateTime('hhmmss',now ) + '.txt'
) ;
myStream.Free;
idHttp_Sina.Free;
Sleep(100);
IsFinished:=True;
inc( fmMain.FinishedTask );
if fmMain.FinishedTask = fmMain.TaskSumNum then
begin
fmMain.mmLog.Lines.Add('多线程完成时间:' + FormatDateTime('hh:mm:ss',now ) );
ShowMessage('所有任务都已完成!');
end;
inherited;
end;
procedure TWeiBoThread.SendID(nID: string);
begin
//
end;
procedure TWeiBoThread.ThreadDone;
begin
exit;
WhatLV.Items[LVIndex].SubItems[1]:=
WhatLV.Items[LVIndex].SubItems[1] + '...ThreadDone:完成:' + FormatDateTime('hh:mm:ss',now ) ;
end;
procedure TWeiBoThread.UpdateListView;
begin
if UpdateFuncID=1 then
WhatLV.Items[LVIndex].SubItems[1]:= inttostr(LVIndex) + ' 开始:' + FormatDateTime('hh:mm:ss',now )
else if UpdateFuncID=2 then
begin
WhatLV.Items[LVIndex].SubItems[1]:=
WhatLV.Items[LVIndex].SubItems[1] + '...完成:' + FormatDateTime('hh:mm:ss',now ) ;
end;
//Synchronize()的作用? 多线程之间控制同步过程.比方说要访问一种资源时,避免同时操作.
//synchronize是不可以带参数的。
end;
//////////////////////
procedure TfmMain.Button1Click(Sender: TObject); //建立线程
var
i:integer;
begin
Caption:= '多线程...';
TaskSumNum:= edtThreadCount.Value;
FinishedTask:=0;
SetLength(MyThreadList, edtThreadCount.Value +1 ); //线程数量
mmLog.Lines.Add('多线程开始时间:' + FormatDateTime('hh:mm:ss',now ) );
for I := 1 to edtThreadCount.Value do
begin
MyThreadList[i] := TWeiBoThread.Create( false );
MyThreadList[i].LVIndex:= i-1;
MyThreadList[i].WhatLV:= lvUserList;
// MyThreadList[i].FreeOnTerminate:=True;
// MyThreadList[i].OnTerminate:= MyThreadList[i].ThreadDone; //
end;
for I := 1 to edtThreadCount.Value do
begin
//MyThreadList[i].Resume;
// MyThreadList[i].Start;
MyThreadList[i].Execute;
end;
mmLog.Lines.Add('多线程呼叫完成,等待线程们处理,呼叫时间:' + FormatDateTime('hh:mm:ss',now ) );
end;
procedure TfmMain.Button3Click(Sender: TObject); //挂起线程 //●●●问题点3
var
i:integer;
begin
for I := 1 to edtThreadCount.Value do
begin
// MyThreadList[i].Suspend;
if MyThreadList[i].Terminated=False then
MyThreadList[i].Suspended:= True;
end;
end;
procedure TfmMain.Button4Click(Sender: TObject); //继续线程
var
i:integer;
begin
for I := 1 to edtThreadCount.Value do
begin
MyThreadList[i].Resume; // .Suspend; //如果线程已终止,则无法暂停止
end;
end;
procedure TfmMain.FormCreate(Sender: TObject);
begin
btnInit.Click;
end;
procedure TfmMain.btnInitClick(Sender: TObject);
var
nIndex,i:integer;
begin
lvUserList.Items.Clear;
nIndex:=0;
for i := 0 to 1000 - 1 do
begin //产生1000个数字序列
inc(nIndex);
with lvUserList.Items.Add do
begin
Caption:= inttostr(nIndex);
SubItems.Add( 'Num' + inttostr(nIndex) ) ;
SubItems.Add( '' ) ;
end;
end;
end;
end.
procedure TWeiBoThread.Execute;
....
Begin
For i:=1 to 任务总数 / 线程数 + 1
Begin
if i >= WhatLV.Items.Count then Exit;
//这里面写原来的那些Execute语句体
End
End;