W
weserver
Unregistered / Unconfirmed
GUEST, unregistred user!
最近小弟学习多线程,模仿做了一个测试的多线程密码扫描软件,不过奇怪的是,软件扫到几个密码后,线程就显示终止了,不知道是为什么???
以下是一些主要的代码,请高手帮忙看一下,并多多指导,谢谢!
主程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Scanner, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
cmdstart: TButton;
cmdstop: TButton;
Memo1: TMemo;
Label1: TLabel;
TxtMin: TEdit;
Label2: TLabel;
TxtMax: TEdit;
IdHTTP1: TIdHTTP;
procedure cmdstopClick(Sender: TObject);
procedure cmdstartClick(Sender: TObject);
procedure WMThreadDone(var Msg:TMessage); message WM_THREADDONEMSG;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
SocketMonkeys:Array[0..MAX_THREADS-1] of ScannerThread;
ThreadsActive:integer;
CurrEndnum,Currnum,Startnum,Endnum:integer;
implementation
{$R *.dfm}
procedure TForm1.WMThreadDone(var Msg:TMessage);
var
DeadThread:Integer;
begin
Dec(ThreadsActive);
DeadThread:=Msg.Wparam; //the ID of the thread that just completed
SocketMonkeys[DeadThread]:=nil;
if ThreadsActive=0 then
memo1.Lines.Add('密码测试完成');
if CMDStop.Enabled then //We haven't been stopped yet
begin
SocketMonkeys[DeadThread]:=ScannerThread.Create(DeadThread,Handle,Currnum,CurrEndnum);
Inc(ThreadsActive);
Currnum:=CurrEndnum+1;
CurrEndnum:=CurrEndnum+10;
if CurrEndnum > Endnum then
begin
CurrEndnum:=Endnum;
CMDStop.Enabled:=false;
cmdstart.Enabled:=true;
end;
end;
Application.ProcessMessages;
end;
procedure TForm1.cmdstopClick(Sender: TObject);
var
i:integer;
begin
cmdstart.Enabled := True;
cmdstop.Enabled := False;
for i:=0 to MAX_THREADS-1 do
SocketMonkeys.Terminate;
end;
procedure TForm1.cmdstartClick(Sender: TObject);
var
i:integer;
begin
memo1.Lines.Clear;
cmdStart.Enabled := false;
cmdstop.Enabled := true;
Caption:='扫描中....';
Startnum:=StrToInt(TxtMin.Text);
Endnum:=StrToInt(TxtMax.Text);
Currnum:=Startnum;
CurrEndnum:=Startnum+10;
if CurrEndnum > Endnum then
CurrEndnum:=Endnum;
for i:=0 to MAX_THREADS-1 do
begin
SocketMonkeys:=ScannerThread.Create(i,Handle,Currnum,CurrEndnum);
inc(ThreadsActive);
Currnum:=CurrEndnum+1;
CurrEndnum:=CurrEndnum+10;
if CurrEndnum > Endnum then
CurrEndnum:=Endnum;
if Currnum >= Endnum then
break;
end;
end;
end.
扫描线程
unit Scanner;
interface
uses
Windows, Messages, Classes,SysUtils,WinInet,Idhttp;
const
MAX_THREADS = 6;
WM_THREADDONEMSG=WM_USER+99;
WM_PORTSTATUS=WM_USER+100;
type
ScannerThread = class(TThread)
private
FWnd: HWND;
FID:Integer;
Fnum:Integer;
Enum:Integer;
Params: TStrings;
htmlstr,showdlg,str1,str2,sFnum1:string;
procedure Testnum;
procedure rmemo;
procedure ThreadTerminate(Sender: TObject);
protected
procedure Execute; override;
public
constructor Create(ID:Integer;Wnd:HWND;Startnum,Endnum:integer);
destructor Destroy; override;
end;
implementation
uses unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure ScannerThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ ScannerThread }
constructor ScannerThread.Create(ID:Integer;Wnd:HWND;Startnum,Endnum:integer);
begin
inherited Create(True);
FreeOnTerminate:=true;
OnTerminate:=ThreadTerminate;
FID:=ID;
FWnd:=Wnd;
Enum:=Endnum;
Fnum:=Startnum;
Resume;
end;
destructor ScannerThread.Destroy;
begin
inherited destroy;
end;
procedure ScannerThread.Testnum;
const
password = '123456';
x = '33';
y = '44';
var
sFnum,strget:string;
IdHTTP:TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
begin
sFnum:=inttostr(Fnum);
sFnum1:='hzxs'+sFnum;
htmlstr:='';
strget:='http://***.***.***';
htmlstr:=IdHttp.get(strget);
end;
except
Terminate; //Something else went wrong - kill the thread
IdHttp.Free;
end;
if Terminated then
Exit;
end;
procedure ScannerThread.rmemo;
begin
form1.Memo1.Lines.Add(sFnum1);
end;
procedure ScannerThread.Execute;
begin
while not Terminated do
begin
Testnum;
if pos(sFnum1,htmlstr)>0 then
Synchronize(rmemo);
Fnum:=Fnum +1;
if Fnum > Enum then
Terminate;
end;
end;
procedure ScannerThread.ThreadTerminate(Sender: TObject);
begin
PostMessage(FWnd,WM_THREADDONEMSG,FID,0);
end;
end.
大家多多帮忙哦,谢谢啦
以下是一些主要的代码,请高手帮忙看一下,并多多指导,谢谢!
主程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Scanner, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
cmdstart: TButton;
cmdstop: TButton;
Memo1: TMemo;
Label1: TLabel;
TxtMin: TEdit;
Label2: TLabel;
TxtMax: TEdit;
IdHTTP1: TIdHTTP;
procedure cmdstopClick(Sender: TObject);
procedure cmdstartClick(Sender: TObject);
procedure WMThreadDone(var Msg:TMessage); message WM_THREADDONEMSG;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
SocketMonkeys:Array[0..MAX_THREADS-1] of ScannerThread;
ThreadsActive:integer;
CurrEndnum,Currnum,Startnum,Endnum:integer;
implementation
{$R *.dfm}
procedure TForm1.WMThreadDone(var Msg:TMessage);
var
DeadThread:Integer;
begin
Dec(ThreadsActive);
DeadThread:=Msg.Wparam; //the ID of the thread that just completed
SocketMonkeys[DeadThread]:=nil;
if ThreadsActive=0 then
memo1.Lines.Add('密码测试完成');
if CMDStop.Enabled then //We haven't been stopped yet
begin
SocketMonkeys[DeadThread]:=ScannerThread.Create(DeadThread,Handle,Currnum,CurrEndnum);
Inc(ThreadsActive);
Currnum:=CurrEndnum+1;
CurrEndnum:=CurrEndnum+10;
if CurrEndnum > Endnum then
begin
CurrEndnum:=Endnum;
CMDStop.Enabled:=false;
cmdstart.Enabled:=true;
end;
end;
Application.ProcessMessages;
end;
procedure TForm1.cmdstopClick(Sender: TObject);
var
i:integer;
begin
cmdstart.Enabled := True;
cmdstop.Enabled := False;
for i:=0 to MAX_THREADS-1 do
SocketMonkeys.Terminate;
end;
procedure TForm1.cmdstartClick(Sender: TObject);
var
i:integer;
begin
memo1.Lines.Clear;
cmdStart.Enabled := false;
cmdstop.Enabled := true;
Caption:='扫描中....';
Startnum:=StrToInt(TxtMin.Text);
Endnum:=StrToInt(TxtMax.Text);
Currnum:=Startnum;
CurrEndnum:=Startnum+10;
if CurrEndnum > Endnum then
CurrEndnum:=Endnum;
for i:=0 to MAX_THREADS-1 do
begin
SocketMonkeys:=ScannerThread.Create(i,Handle,Currnum,CurrEndnum);
inc(ThreadsActive);
Currnum:=CurrEndnum+1;
CurrEndnum:=CurrEndnum+10;
if CurrEndnum > Endnum then
CurrEndnum:=Endnum;
if Currnum >= Endnum then
break;
end;
end;
end.
扫描线程
unit Scanner;
interface
uses
Windows, Messages, Classes,SysUtils,WinInet,Idhttp;
const
MAX_THREADS = 6;
WM_THREADDONEMSG=WM_USER+99;
WM_PORTSTATUS=WM_USER+100;
type
ScannerThread = class(TThread)
private
FWnd: HWND;
FID:Integer;
Fnum:Integer;
Enum:Integer;
Params: TStrings;
htmlstr,showdlg,str1,str2,sFnum1:string;
procedure Testnum;
procedure rmemo;
procedure ThreadTerminate(Sender: TObject);
protected
procedure Execute; override;
public
constructor Create(ID:Integer;Wnd:HWND;Startnum,Endnum:integer);
destructor Destroy; override;
end;
implementation
uses unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure ScannerThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ ScannerThread }
constructor ScannerThread.Create(ID:Integer;Wnd:HWND;Startnum,Endnum:integer);
begin
inherited Create(True);
FreeOnTerminate:=true;
OnTerminate:=ThreadTerminate;
FID:=ID;
FWnd:=Wnd;
Enum:=Endnum;
Fnum:=Startnum;
Resume;
end;
destructor ScannerThread.Destroy;
begin
inherited destroy;
end;
procedure ScannerThread.Testnum;
const
password = '123456';
x = '33';
y = '44';
var
sFnum,strget:string;
IdHTTP:TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
begin
sFnum:=inttostr(Fnum);
sFnum1:='hzxs'+sFnum;
htmlstr:='';
strget:='http://***.***.***';
htmlstr:=IdHttp.get(strget);
end;
except
Terminate; //Something else went wrong - kill the thread
IdHttp.Free;
end;
if Terminated then
Exit;
end;
procedure ScannerThread.rmemo;
begin
form1.Memo1.Lines.Add(sFnum1);
end;
procedure ScannerThread.Execute;
begin
while not Terminated do
begin
Testnum;
if pos(sFnum1,htmlstr)>0 then
Synchronize(rmemo);
Fnum:=Fnum +1;
if Fnum > Enum then
Terminate;
end;
end;
procedure ScannerThread.ThreadTerminate(Sender: TObject);
begin
PostMessage(FWnd,WM_THREADDONEMSG,FID,0);
end;
end.
大家多多帮忙哦,谢谢啦