多线程的问题(100分)

  • 主题发起人 主题发起人 chenge1980
  • 开始时间 开始时间
C

chenge1980

Unregistered / Unconfirmed
GUEST, unregistred user!
每个线程用AllocateHWnd建立窗体句柄,用getmeeeage()和 DispatchMessage()建立消息循环,用SetTime和winsockAPI建立定时器和通信连接。<br>问:在定时器事件中不能关闭winsock(CloseSocket),在socket事件中不能关闭Timer(KillTimer)<br>非常迷茫
 
为什么不能关闭???? 说说错误原因
 
返回的结果是0,但还是会触发OnTimer和OnSocket
 
那说明你killtimer没成功啊,成功返回非0
 
代码贴出来看看。
 
但它返回的值确实是0,而且在Execute里相同的语句可以正确的执行,兄弟有MSN吗??
 
zjttcsy@163.com
 
精简了一下,可能有错的<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; TWinSockThread=class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; CliAddrIn:TSockAddrIn; &nbsp; &nbsp;//winsock地址结构(监测系统IP地址、端口)<br>&nbsp; &nbsp; procedure OnTimer; &nbsp; &nbsp; &nbsp; &nbsp;//定时器事件<br>&nbsp; &nbsp; procedure OnSocket(Event:Word); //WinSock事件<br>&nbsp; &nbsp; procedure WndProc(var Message: TMessage); &nbsp;//窗体过程<br>&nbsp; &nbsp; procedure SockConnect; &nbsp; &nbsp;//连接<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute;override;<br>&nbsp; public<br>&nbsp; &nbsp; TimerID:Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//定时器编号<br>&nbsp; &nbsp; WinSockThreadHandle:HWnd; //隐含窗体句柄<br>&nbsp; &nbsp; Connected:Boolean; &nbsp; &nbsp; &nbsp; &nbsp;//是否连接<br>&nbsp; &nbsp; CliSocket:Integer; &nbsp; &nbsp; &nbsp; &nbsp;//WinsockID<br>end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure TWinSockThread.Execute;<br>var<br>&nbsp; Msg:TMsg;<br>begin<br>&nbsp; FreeOnTerminate:=True;<br>&nbsp; WinSockThreadHandle:=Classes.AllocateHWnd(WndProc);<br>&nbsp; WSAStartup(MAKEWORD(2,2),WSAData);<br>&nbsp; Connected:=True;<br>&nbsp; SockConnect;<br>&nbsp; TimerID:=SetTimer(WinSockThreadHandle,0,3000,nil);<br>&nbsp; while GetMessage(Msg,WinSockThreadHandle,0,0) do<br>&nbsp; &nbsp; DispatchMessage(Msg);<br>&nbsp; KillTimer(WinSockThreadHandle,TimerID);<br>&nbsp; CloseSocket(CliSocket);<br>&nbsp; WSACleanUP();<br>&nbsp; Classes.DeallocateHWnd(WinSockThreadHandle);<br>end;<br><br>procedure TWinSockThread.OnSocket(Event:Word);<br>var<br>&nbsp; Buf:array[0..1023] of Byte;<br><br>begin<br>&nbsp; KillTimer(WinSockThreadHandle,TimerID);<br>&nbsp; case Event of<br>&nbsp; &nbsp; FD_READ: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//读取数据<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;//操作<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br><br>procedure TWinSockThread.SockConnect; //连接<br>var<br>&nbsp; err:Integer;<br>begin<br>&nbsp; CliSocket := socket(AF_INET, SOCK_STREAM,IPPROTO_IP);<br>&nbsp; if (CliSocket = INVALID_SOCKET) then<br>&nbsp; begin<br>&nbsp; &nbsp; CloseSocket(CliSocket);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; ZeroMemory(@CliAddrIn,sizeof(CliAddrIn));<br>&nbsp; CliAddrIn.sin_addr.s_addr:=Inet_Addr(PChar(SockIP));<br>&nbsp; CliAddrIn.sin_family := AF_INET;<br>&nbsp; CliAddrIn.sin_port :=Htons(SockPort);<br>&nbsp; &nbsp; err:=Connect(CliSocket,CliAddrIn, SizeOf(CliAddrIn));<br>&nbsp; &nbsp; if err = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Connected:=True;<br>// &nbsp; &nbsp; Shape.Brush.Color:=clLime;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; //Form1.ListBox2.Items.Add(IntToStr(WSAGetLastError()));<br>&nbsp; &nbsp; // &nbsp;Connected:=False;<br>&nbsp; &nbsp; end; &nbsp; &nbsp; //绑定消息映射<br>&nbsp; &nbsp; WSAAsyncSelect(CliSocket,WinSockThreadHandle, WM_SOCK, FD_READ or FD_Accept or FD_CONNECT or FD_WRITE or FD_CLOSE);<br>end;<br><br>procedure TWinSockThread.OnTimer;//定时器事件<br>var<br>&nbsp; errclose:integer;<br>begin<br>&nbsp; errclose:=CloseSocket(CliSocket);<br>end;<br><br>procedure TWinSockThread.WndProc(var Message: TMessage); //隐含窗体过程<br>begin<br>&nbsp; case Message.Msg of<br>&nbsp; &nbsp; WM_Timer:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; OnTimer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //响应定时器事件<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; WM_Sock:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; OnSocket(WSAGetSelectEvent(Message.LParam)); //响应WinSock事件<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>end.<br>
 
后退
顶部