我是使用timer控件来控制TcpClient从tcpServer读入数据的 Msg := IdTCPClient1.ReadLn('', 5);但是timer控件有一定的时间限止,比如说,我的是1000毫秒,请问,我怎么样才能使tcpClient像tcpServer一样,有一个OnExecute事件,即indyTcpClient只有当接收到TcpServer发来的消息时,才会触发事件,而其它时间是不会触发事件的。谢谢
解决方案 »
- 求两个时间之间相差天数的问题?
- 求教一个dll静态释放的问题,请给出解决代码!
- 删除本人回复的大版出来!!!!!!!!
- Fastreport 中打印时如何让其不显示打印对话框?
- 排列组合的问题,高分求解!不够再加
- 在Delphi中怎樣用SQL語句得到某張表中A字段值為最大的B字段的值?
- PB能否调用Delphi做的DLL,如果能,DELPHI中的STRING声明的变量在PB中怎么对应??
- 请教个问题:没有源代码能否有办法调整*.QRP报表格式?存为QRP文件后能否有什么程序可以编辑
- 急!急!急!VC++语句用DELPHI如何表示,高分回送先送40
- 请教!我要读入一个很多行的列表,可是ListBox只能读入3万多行,我该怎么办呢?
- 大家帮忙看一段程序,为什么得到的压缩数据好像还是很大?? 哪里些错了?
- 双击一个控件,却生成不了procedure
我这里用消息来通知主窗体,你可以用回调函数,这个很灵活的。
unit Thread;interfaceuses
Classes, IdTCPClient, Windows, Messages;type
TRecvThread = class(TThread)
private
FTCPClient: TIdTCPClient;
FHandle: Cardinal;
protected
procedure Execute; override;
public
constructor Create(TCPClient: TIdTCPClient; hWnd: Cardinal);
end;implementation{ TRecvThread }constructor TRecvThread.Create(TCPClient: TIdTCPClient; hWnd: Cardinal);
begin
inherited Create(false);
FreeOnTerminate:=true;
FTCPClient:=TCPClient;
FHandle:=hWnd;
end;procedure TRecvThread.Execute;
var
Count: Integer;
P: PByte;
begin
while (not Terminated) and FTCPClient.Connected do
begin
Count:=FTCPClient.ReadFromStack(false);
if Count<>0 then
begin
GetMem(P, Count);
FTCPClient.ReadBuffer(P^, Count);
SendMessage(FHandle, WM_USER+1000, Integer(P), Count);
FreeMem(P, Count);
end;
end;
end;end.主窗体示例(部分):
uses Thread;procedure TForm1.FormCreate(Sender: TObject);
begin
TRecvThread.Create(IdTCPClient1, Handle);
end;//消息处理函数
procedure TForm1.Recv(var Msg: TMessage);
beginend;
ires,i : integer;
recMsg : TMyMsg; //一个record
begin
{ Place thread code here }
FreeOnTerminate := True; while ((not Terminated) and DM.tcpLog.Connected) do
begin
ires := DM.tcpLog.ReadFromStack;
if ires > 0 then
begin
DM.tcpLog.ReadBuffer(recMsg,sizeof(recMsg));
case recMsg.iType of
3 :
begin
frm_clientMain.meorec.Lines.Add(recMsg.UserName+' 说:');
frm_clientMain.meorec.Lines.Add(recMsg.sMsg);
end;
2 :
begin
i := Namelist.IndexOf(recMsg.UserName);
if i > -1 then
begin
StatList.Strings[i] := IntToStr(recMsg.istat);
frm_clientMain.ListView1.Items[i].ImageIndex := recMsg.istat;
frm_clientMain.ListView1.Repaint;
end;
end;
end;
end;
end;
end;
这是我的,已经成功