procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
Lists :TStrings;
i,j :integer;
ClientIP,SqlText :string;
begin
  with AThread.Connection do
  begin
    Lists := TStringList.Create;
    AThread.Connection.ReadStrings(Lists);
    if Lists.Count>0 then
    begin
      with ListView1.Items.Add do
      begin
        i := AThread.ThreadID;
        ClientIP := AThread.Connection.Socket.Binding.PeerIP;
        j := ListView1.Items.Count;
        ListView1.Items[j-1].Caption := InttoStr(i);
        ListView1.Items[j-1].SubItems.Add(DateTimeToStr(Now));
        ListView1.Items[j-1].SubItems.Add(Lists.Strings[0]);
        ListView1.Items[j-1].SubItems.Add(Lists.Strings[1]);
        ListView1.Items[j-1].SubItems.Add(AThread.Connection.Socket.Binding.PeerIP);
        ListView1.Items[j-1].SubItems.Add(Lists.Strings[2]);
      end;
      if Lists.Strings[2]='FTP已连接' then
      begin
        SqlText := 'select * from retmxls where fdbs='+''''+Lists.Strings[0]+'''';
        TSQLThread.Create(False,SqlText,ClientIP,Lists,i);
      end;
    end;
  end;
end;当下面的子线程结束后怎么把消息发送给相应的客户端
unit Unit2;interfaceuses
  Classes, ADODB, Dialogs, SysUtils, StdCtrls, Activex, ComCtrls, IdThread,
  Inifiles, Forms, IdComponent, IdTCPServer;type
  TSQLThread = class(TThread)
  private
    FSQLConnection :TADOConnection;
    FSQLQuery :TADOQuery;
    FClientIP,FServerName,FDBName,FUserName,FPassword :String;
    i,FId,RecCount :integer;
    TMSList :TStrings;
    { Private declarations }
  protected
    procedure UpdateListView(FFlag: integer; ErrorMsg: string);
    procedure Execute; override;
  public
    constructor Create(CreateSuspended:Boolean; SqlText,ClientIP: String;
                      TMSLists: TStrings; ThreadID: integer);virtual;
  end;implementationuses 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 TSQLThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ TSQLThread }procedure TSQLThread.UpdateListView(FFlag: integer; ErrorMsg: string);
begin
  i := Form1.ListView1.Items.Count;
  case FFlag of
    1:
      begin
        with Form1.ListView1.Items.Add do
        begin
          Form1.ListView1.Items[i].Caption := IntToStr(FId);
          Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
          Form1.ListView1.Items[i].SubItems.Add(FClientIP);
          Form1.ListView1.Items[i].SubItems.Add('客户端正在连接SQL服务器');
        end;
        Form1.ListView1.Update;
        Form1.ListView1.Refresh;
      end;
    2:
      begin
        with Form1.ListView1.Items.Add do
        begin
          Form1.ListView1.Items[i].Caption := IntToStr(FId);
          Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
          Form1.ListView1.Items[i].SubItems.Add(FClientIP);
          Form1.ListView1.Items[i].SubItems.Add('客户端正在执行SQL数据导出');
        end;
        Form1.ListView1.Update;
        Form1.ListView1.Refresh;
      end;
    3:
      begin
        with Form1.ListView1.Items.Add do
        begin
          Form1.ListView1.Items[i].Caption := IntToStr(FId);
          Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
          Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
          Form1.ListView1.Items[i].SubItems.Add(FClientIP);
          Form1.ListView1.Items[i].SubItems.Add('客户端执行SQL数据导出完成');
          Form1.ListView1.Items[i].SubItems.Add('数据Total:'+IntToStr(RecCount));
        end;
        Form1.ListView1.Update;
        Form1.ListView1.Refresh;
      end;
      4:
        begin
          with Form1.ListView1.Items.Add do
          begin
            Form1.ListView1.Items[i].Caption := 'Server';
            Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
            Form1.ListView1.Items[i].SubItems.Add('000');
            Form1.ListView1.Items[i].SubItems.Add('SQLSERVER服务器');
            Form1.ListView1.Items[i].SubItems.Add('');
            Form1.ListView1.Items[i].SubItems.Add('SQL服务器连接错误');
            Form1.ListView1.Items[i].SubItems.Add('Error:'+ErrorMsg);
          end;
        end;
      5:
        begin
          with Form1.ListView1.Items.Add do
          begin
            Form1.ListView1.Items[i].Caption := 'Server';
            Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
            Form1.ListView1.Items[i].SubItems.Add('000');
            Form1.ListView1.Items[i].SubItems.Add('SQLSERVER服务器');
            Form1.ListView1.Items[i].SubItems.Add('');
            Form1.ListView1.Items[i].SubItems.Add('数据导出操作错误');
            Form1.ListView1.Items[i].SubItems.Add('Error:'+ErrorMsg);
          end;
        end;
  end;
end;constructor TSQLThread.Create(CreateSuspended:Boolean; SqlText,ClientIP: String;
                              TMSLists: TStrings; ThreadId: integer);
var
FFileName :string;
begin
  inherited Create(CreateSuspended);
  CoInitialize(nil);
  TMSList := TMSLists;
  FId := ThreadId;
  FClientIP := ClientIP;
  FFileName := ExtractFilePath(Application.ExeName)+'ini\DtsServer.ini';
  with TInifile.Create(FFileName) do
  begin
    try
      FServerName := ReadString('SQL服务器','ServerName','');
      FDBname := ReadString('SQL服务器','DataBaseName','');
      FUserName := ReadString('SQL服务器','UserName','');
      FPassword := ReadString('SQL服务器','Password','');
    except
      on E:Exception do
      begin
        ShowMessage(e.Message);
        free;
      end;
    end;
  end;
  FSQLConnection := TADOConnection.Create(nil);
  FSQLConnection.LoginPrompt := False;
  FSQLConnection.ConnectionString :='Provider=SQLOLEDB.1;User ID='
                                    +trim(FUserName)+';Password='
                                    +trim(FPassword)+';Initial Catalog='
                                    +trim(FDBName)+';Data Source='
                                    +trim(FServerName);
  FSQLQuery := TADOQuery.Create(nil);
  FSQLQuery.Connection := FSQLConnection;
  FSQLQuery.SQL.Add(SqlText);
  FreeOnTerminate := True;
end;procedure TSQLThread.Execute;
begin
  try
    UpdateListView(1,'');
    FSQLConnection.ConnectionTimeout := 15;
    FSQLConnection.Open;
    try
      UpdateListView(2,'');
      FSQLQuery.Open;
      RecCount := FSQLQuery.RecordCount;
      FSQLQuery.Close;
      FSQLConnection.Close;
      UpdateListView(3,'');
    except
      on E:Exception do
      begin
        FSQLQuery.Close;
        FSQLConnection.Close;
        UpdateListView(5,E.Message);
      end;
    end;
  except
      on E:Exception do
      begin
        FSQLQuery.Close;
        FSQLConnection.Close;
        UpdateListView(4,E.Message);
      end;
  end;
  { Place thread code here }
end;end.

解决方案 »

  1.   

    这么简单的一条SQL查询语句为什么专门开一个线程来处理呢,做一个子查询就可以了啊
      

  2.   

    多客户端 执行相同查询语句 但参数不同 所以要用线程来解决哦问题已经解决。把AThread 传入子线程就可以了 解决了不同客户端并发查询 同时向对应客户端发送信息 第一次接触idtcpserver 不知道有没有更方便的写法。unit Unit2;interfaceuses
      Classes, ADODB, Dialogs, SysUtils, StdCtrls, Activex, ComCtrls, IdThread,
      Inifiles, Forms, IdComponent, IdTCPServer;type
      TSQLThread = class(TThread)
      private
        FSQLConnection :TADOConnection;
        FSQLQuery :TADOQuery;
        FClientIP,FServerName,FDBName,FUserName,FPassword :String;
        i,FId,RecCount :integer;
        TMSList :TStrings;
        F_Thread :TIdPeerThread;
        { Private declarations }
      protected
        procedure UpdateListView(FFlag: integer; ErrorMsg: string);
        procedure Execute; override;
      public
        constructor Create(CreateSuspended:Boolean; SqlText,ClientIP: String;
                          TMSLists: TStrings; ThreadID: integer;
                          FThread: TIdPeerThread);virtual;
      end;implementationuses 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 TSQLThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TSQLThread }procedure TSQLThread.UpdateListView(FFlag: integer; ErrorMsg: string);
    begin
      i := Form1.ListView1.Items.Count;
      case FFlag of
        1:
          begin
            with Form1.ListView1.Items.Add do
            begin
              Form1.ListView1.Items[i].Caption := IntToStr(FId);
              Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
              Form1.ListView1.Items[i].SubItems.Add(FClientIP);
              Form1.ListView1.Items[i].SubItems.Add('客户端正在连接SQL服务器');
            end;
            Form1.ListView1.Update;
            Form1.ListView1.Refresh;
          end;
        2:
          begin
            with Form1.ListView1.Items.Add do
            begin
              Form1.ListView1.Items[i].Caption := IntToStr(FId);
              Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
              Form1.ListView1.Items[i].SubItems.Add(FClientIP);
              Form1.ListView1.Items[i].SubItems.Add('客户端正在执行SQL数据导出');
            end;
            Form1.ListView1.Update;
            Form1.ListView1.Refresh;
          end;
        3:
          begin
            with Form1.ListView1.Items.Add do
            begin
              Form1.ListView1.Items[i].Caption := IntToStr(FId);
              Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[0]);
              Form1.ListView1.Items[i].SubItems.Add(TMSList.Strings[1]);
              Form1.ListView1.Items[i].SubItems.Add(FClientIP);
              Form1.ListView1.Items[i].SubItems.Add('客户端执行SQL数据导出完成');
              Form1.ListView1.Items[i].SubItems.Add('数据Total:'+IntToStr(RecCount));
            end;
            Form1.ListView1.Update;
            Form1.ListView1.Refresh;
            F_Thread.Connection.WriteLn('Successed');
          end;
          4:
            begin
              with Form1.ListView1.Items.Add do
              begin
                Form1.ListView1.Items[i].Caption := 'Server';
                Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
                Form1.ListView1.Items[i].SubItems.Add('000');
                Form1.ListView1.Items[i].SubItems.Add('SQLSERVER服务器');
                Form1.ListView1.Items[i].SubItems.Add('');
                Form1.ListView1.Items[i].SubItems.Add('SQL服务器连接错误');
                Form1.ListView1.Items[i].SubItems.Add('Error:'+ErrorMsg);
              end;
            end;
          5:
            begin
              with Form1.ListView1.Items.Add do
              begin
                Form1.ListView1.Items[i].Caption := 'Server';
                Form1.ListView1.Items[i].SubItems.Add(DateTimeToStr(Now));
                Form1.ListView1.Items[i].SubItems.Add('000');
                Form1.ListView1.Items[i].SubItems.Add('SQLSERVER服务器');
                Form1.ListView1.Items[i].SubItems.Add('');
                Form1.ListView1.Items[i].SubItems.Add('数据导出操作错误');
                Form1.ListView1.Items[i].SubItems.Add('Error:'+ErrorMsg);
              end;
            end;
      end;
    end;constructor TSQLThread.Create(CreateSuspended:Boolean; SqlText,ClientIP: String;
                                  TMSLists: TStrings; ThreadId: integer;
                                  FThread: TIdPeerThread);
    var
    FFileName :string;
    begin
      inherited Create(CreateSuspended);
      CoInitialize(nil);
      TMSList := TMSLists;
      FId := ThreadId;
      FClientIP := ClientIP;
      F_Thread := FThread;
      FFileName := ExtractFilePath(Application.ExeName)+'ini\DtsServer.ini';
      with TInifile.Create(FFileName) do
      begin
        try
          FServerName := ReadString('SQL服务器','ServerName','');
          FDBname := ReadString('SQL服务器','DataBaseName','');
          FUserName := ReadString('SQL服务器','UserName','');
          FPassword := ReadString('SQL服务器','Password','');
        except
          on E:Exception do
          begin
            ShowMessage(e.Message);
            free;
          end;
        end;
      end;
      FSQLConnection := TADOConnection.Create(nil);
      FSQLConnection.LoginPrompt := False;
      FSQLConnection.ConnectionString :='Provider=SQLOLEDB.1;User ID='
                                        +trim(FUserName)+';Password='
                                        +trim(FPassword)+';Initial Catalog='
                                        +trim(FDBName)+';Data Source='
                                        +trim(FServerName);
      FSQLQuery := TADOQuery.Create(nil);
      FSQLQuery.Connection := FSQLConnection;
      FSQLQuery.SQL.Add(SqlText);
      FreeOnTerminate := True;
    end;procedure TSQLThread.Execute;
    begin
      try
        UpdateListView(1,'');
        FSQLConnection.ConnectionTimeout := 15;
        FSQLConnection.Open;
        try
          UpdateListView(2,'');
          FSQLQuery.Open;
          RecCount := FSQLQuery.RecordCount;
          FSQLQuery.Close;
          FSQLConnection.Close;
          UpdateListView(3,'');
        except
          on E:Exception do
          begin
            FSQLQuery.Close;
            FSQLConnection.Close;
            UpdateListView(5,E.Message);
          end;
        end;
      except
          on E:Exception do
          begin
            FSQLQuery.Close;
            FSQLConnection.Close;
            UpdateListView(4,E.Message);
          end;
      end;
      { Place thread code here }
    end;end.