多线程关于线程退出的问题。(100分)

  • 主题发起人 主题发起人 kernelj
  • 开始时间 开始时间
K

kernelj

Unregistered / Unconfirmed
GUEST, unregistred user!
线程代码:
代码:
unit Unit2;
interface
uses
  Classes, SysUtils, Winsock, Windows,ExtCtrls,StdCtrls;
type
  chkconn = class(TThread)
  private
    { Private declarations }
    strs:TStringList;
    TmpMsgStr: TMemo;
    Tmpip:string;
    proceduredo
save;
    //proceduredo
save1;
  protected
    procedure Execute;
override;
  public
    constructor Ctreate(ip:string;MsgStr: TMemo);
  end;

implementation
{ 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 chkconn.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end;
}
{ chkconn }
var
  CS: TRTLCriticalSection;
constructor chkconn.Ctreate(ip:string;MsgStr: TMemo);
//构造线程
begin
  Tmpip := ip;
  TmpMsgStr := MsgStr;
  //strs.LoadFromFile(ExtractFilePath(Application.ExeName)+'server.ini');
  InitializeCriticalSection(CS);
//初始化
  FreeOnTerminate := True;
//删除
  inherited Create(False);
//运行
end;

function Chkconn1(ip:string):Boolean;
var
  MyClientSock:TSocket;
  Socket5Proxy:TSockAddr;
  MySocketBuf:array[0..256]of byte;
  PcharSocketAddr:PChar;
  Re:integer;
  TempWSAData:TWSAData;
begin
  Result:=false;
  WSAStartup(MAKEWORD(2,0),TempWSAData);
  MyClientSock:=socket(AF_INET,SOCK_STREAM,0);
  if(MyClientSock<>INVALID_SOCKET) then
  begin
    ZeroMemory(@Socket5Proxy,sizeof(Socket5Proxy));
    Socket5Proxy.sin_family := AF_INET;
    GetMem(PcharSocketAddr,Length(ip)+1);
    ZeroMemory(PcharSocketAddr,Length(ip)+1);
    StrPCopy(PcharSocketAddr,ip);
    Socket5Proxy.sin_addr.S_addr :=inet_addr(PcharSocketAddr);
    FreeMem(PcharSocketAddr);
    Socket5Proxy.sin_port := htons(StrToInt('3000'));
    Re:=connect(MyClientSock,Socket5Proxy,sizeof(Socket5Proxy));
    if Re <> SOCKET_ERROR then
 Result:=true;
  end;
  closesocket(MyClientSock);
end;

procedure chkconn.dosave;
//var
  //s: string;
begin
  //s:=s+Tmpip+',';
  //strs.CommaText:=s;
  TmpMsgStr.Lines.Add(Tmpip);
end;

procedure chkconn.Execute;
begin
  { Place thread code here }
  sleep(10);
  EnterCriticalSection(cs);
  if ChkConn1(Tmpip) then
    Synchronize(dosave);
  LeaveCriticalSection(CS);
  FreeOnTerminate := True;
end;

end.
主程序代码:
代码:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Winsock, Unit2;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Timer1: TTimer;
    //procedure Timer1Timer(Sender: TObject);
    //function Chkconn(ip:string):Boolean;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation
{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
  strss:Tstringlist;
  i: integer;
  ThreadScan: chkconn;
//定义线程变量
begin
  strss:=Tstringlist.Create;
  strss.LoadFromFile(ExtractFilePath(Application.ExeName)+'server.ini');
  for i := 0 to strss.Count - 1do
  begin
 
    ThreadScan := chkconn.Ctreate(strss[i],Memo1);
    sleep(10);
//挂起
  end;
  strss.Free;
  //Timer1.Enabled:=true;
end;

end.
问题描述,每执行一次扫描,就会增加几个线程,无法自动结束,知道关闭程序。请求帮助。另外我想把扫描到的ip一个一行的写入文件中,应该怎么做。感谢了。
 
扫描?什么扫描..都没有这些代码...只是看到你的线程在执行...而且也是第一次看这样写建立Socket的,连清除也没有....看到你现在的功能,只是想检查能不能连接上对方而已...
assignfile(f,filename);
if fileexits(filename) then
reset(f)
else
rewrite(f);
seek(f,filesize(f));
write(你的内容);
closefile(f);
全手写的,没开Delphi...自己注意一下...
 
代码写的太诡异了
 
[:(]代码很有我问题吗?
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
520
import
I
后退
顶部