unit OpenLampThrd;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls, ExtCtrls, ComCtrls;
type
  TOpenLampThread=class(Tthread)
  private
    thclient_socket:tclientsocket;{该线程使用的Clientsocket对象}
    procedure onconn(Sender:TObject;Socket: TCustomWinSocket);
  protected
    procedure execute;override;
  public
    constructor create(var thdclient_socket: tclientsocket);
  end;  implementation
uses StreetLamp, DM, Main;{ OpenLampThread }
constructor TOpenLampThread.create(var thdclient_socket: tclientsocket);
begin
  inherited create(false);
  freeonterminate:=true;
  Resume;
end;procedure TOpenLampThread.execute;
var
  i:integer;
begin
    inherited create(false);
  thclient_socket:=tclientsocket.create(nil);
  thclient_socket.Active:=false;
  thclient_socket.Address:='192.168.0.144';
  thclient_socket.Port:=10002;
  thclient_socket.OnConnect:=onconn;
  thclient_socket.ClientType:=ctNonBlocking;
   thclient_socket.Active:=true;//问题:此句执行后其Active属性还是False.调试时
//光标放在此行Active上,有时显示thclient_socket.Active=false,有时根本没显示。
   i:=29;
thclient_socket.Socket.SendBuf(i,SizeOf(i));  sleep(500);
 
end;procedure TOpenLampThread.onconn(Sender:TObject;Socket: TCustomWinSocket);
var       //这段代码因为 thclient_socket.Active不能置为true以至不能执行;
  i:integer;
begin
i:=29;
thclient_socket.Socket.SendBuf(i,SizeOf(i));
end;end.在主程序中调用线程
procedure TfrmMain.tbtnThreadOpenClick(Sender: TObject);
begin
TOpenLampThread.create(frmstreetLamp.ClientSocket3);
end;
为什么不能改变在线程中自动生成的ClientSocket的Active属性?

解决方案 »

  1.   

    constructor TOpenLampThread.create(var thdclient_socket: tclientsocket);
    begin
      thclient_socket:=thdclient_socket;
      freeonterminate:=true;
      Resume;
      inherited create(false);
    end;即使这样解决了,你的程序还有好长的路要走。socket还不知道连上了没有,你的线程等了500毫秒就结束了!sleep又容易引起时间上的紊乱!
      

  2.   

    xyqxj(蓝泥) :改用你的代码,还是和原来的出错情况一样。
      

  3.   

    我是没有办法了,就这样吧,你的ip,端口号都要读进来才行,定死了以后还怎么玩啊!
    要是不嫌麻烦,把读出来的值输出到主线程,判断返回了正确的值了没有,如不正确再创建线程收啊收,要是还收不到,把时间延长点,让它sleep(500×2),然后再...
      

  4.   

    为什么不放到主线程里面呢,socket通讯好像不怎么占用时间的。
      

  5.   

    帮忙把我的顶一顶,我的问题是:“Websnap入门路上体验CGI遭遇500错误! ”,谢谢!
      

  6.   

    试试
    thclient_socket.ClientType:=ctBlocking;线程结束
    freeonterminate:=false;
      

  7.   

    new 一个Timer,然后把Timer的Enabled 设为true,在Timer的事件里面把TClientSocket的Active = True,或者调用它的Open();
      

  8.   

    在Timer的实践里面先把Timer设为false
      

  9.   

    1,非阻塞的TClientSocket不能那样在非主线程工作.
    要不改用阻塞方式的2,要不就在主线程,非阻塞方式的SOCKET不会阻塞主线程的.