简体   繁体   English

Delphi TWSocket带螺纹

[英]Delphi TWSocket with Thread

When i used this code with button, it is ok... All sockets connected success. 当我将此代码与按钮一起使用时,就可以了...所有套接字连接成功。 All events working. 所有事件均有效。 no problem (OnSessionClosed, OnSessionConnected, ...) 没问题(OnSessionClosed,OnSessionConnected等)

procedure TfrmMain.btnConnectClick(Sender: TObject);
var
  I: Integer;
begin    
  for I := 0 to query.RecordCount - 1 do
  begin
    pUser[I] := TUser.Create();
    pUser[I].Connect(frmMain.editEbenezerIP.Text);
    pUser[I].run := False;
    pUser[I].username := Trim(query.FieldByName('strAccountID').Text);
    pUser[I].password := Trim(query.FieldByName('strPasswd').Text);
    pUser[I].md5 := editMD5.Text;
    pUser[I].Resume;
    query.Next;
  end;
end;

i created a thread to connect with sleep. 我创建了一个线程来连接睡眠。 (my thread TConnector). (我的线程TConnector)。 All thread connected but OnSessionConnected event not working when i created with TConnector. 当我使用TConnector创建时,所有连接的线程但OnSessionConnected事件不起作用。

No problem with button to use create sockets. 使用创建套接字的按钮没有问题。

procedure TUser.OnSessionConnected(Sender: TObject; ErrCode: Word);
begin
     ShowMessage('Connection success!');
end;

procedure TUser.Connect(eip : string);
begin
  Initialize;
  socket := TWSocket.Create(nil);
  socket.OnDataAvailable    := OnDataAvailable;
  socket.OnSessionConnected := OnSessionConnected;
  socket.OnSessionClosed    := OnSessionClosed;
  socket.Connect;
end;

procedure TConnector.Execute;
var
  I : Integer;
  pUser : array [0..1500] of TUser;
begin
  for I := 0 to frmMain.query.RecordCount - 1 do
  begin
    pUser[I] := TUser.Create();
    pUser[I].run := False;
    pUser[I].username := Trim(frmMain.query.FieldByName('strAccountID').Text);
    pUser[I].password := Trim(frmMain.query.FieldByName('strPasswd').Text);
    pUser[I].Connect(frmMain.editEbenezerIP.Text);
    pUser[I].Resume;
    frmMain.query.Next;
    **Sleep(100);**
  end;
end;

I fixed this problem with Synchronize(CreateUser); 我用Synchronize(CreateUser)解决了这个问题 . Thanks for your answers 谢谢你的回答

  TConnector = class(TThread)
  private

  protected
    procedure Execute; override;
  public
    strAccountID, strPasswd, MD5, eIP : string;
    X : Integer;
    constructor Create;
    procedure CreateUser;
  end;

procedure TConnector.CreateUser;
begin
    Output(Format('Thread for %s',[strAccountID]));
    frmMain.pUser[X] := TUser.Create();
    frmMain.pUser[X].run := False;
    frmMain.pUser[X].username := strAccountID;
    frmMain.pUser[X].password := strPasswd;
    frmMain.pUser[X].md5 := MD5;
    frmMain.pUser[X].Connect(eIP, frmMain);
    frmMain.pUser[X].Resume;

end;

procedure TConnector.Execute;
var
   I : Integer;
begin
  MD5          := frmMain.editMD5.Text;
  eIP          := frmMain.editEbenezerIP.Text;
  for I := 0 to frmMain.query.RecordCount - 1 do
  begin
    X := I;
    strAccountID := Trim(frmMain.query.FieldByName('strAccountID').Text);
    strPasswd    := Trim(frmMain.query.FieldByName('strPasswd').Text);

    **Synchronize(CreateUser);**

    Sleep(1000);
    frmMain.query.Next;
  end;

  while(not Terminated)do
  begin
    Sleep(1000);
    OutPut('test');
  end;
end;

TWSocket uses a non-blocking socket and a hidden window for handling socket state updates asynchronously. TWSocket使用非阻塞套接字和隐藏窗口来异步处理套接字状态更新。 As such, you need to give your thread a message loop. 因此,您需要给线程一个消息循环。 It works in a TButton.OnClick event because it is utilizing the main thread's existing message loop. 它在TButton.OnClick事件中工作,因为它利用了主线程的现有消息循环。

Edit: The simplest message loop involves calling Peek/GetMessage() , TranslateMessage() , and DispatchMessage() in a loop for the lifetime of the thread, so you need to add those function calls to your worker thread, eg: 编辑:最简单的消息循环涉及在线程的整个生命周期Peek/GetMessage()循环中调用Peek/GetMessage()TranslateMessage()DispatchMessage() ,因此您需要将这些函数调用添加到工作线程中,例如:

procedure TConnector.Execute;
var
  I : Integer;
  pUser : array [0..1500] of TUser;
  Msg: TMsg
begin
  for I := 0 to frmMain.query.RecordCount - 1 do
  begin
    if Terminated then Break;
    pUser[I] := TUser.Create();
    pUser[I].run := False;
    pUser[I].username := Trim(frmMain.query.FieldByName('strAccountID').Text);
    pUser[I].password := Trim(frmMain.query.FieldByName('strPasswd').Text);
    pUser[I].Connect(frmMain.editEbenezerIP.Text);
    pUser[I].Resume;
    frmMain.query.Next;
  end;

  while (GetMessage(Msg, 0, 0, 0) > 0) and (not Terminated) then
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;

  // perform cleanup here as needed...
end;

procedure TConnector.Stop;
begin
  Terminate;
  PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM