除非中间有一个 MessageBox,否则 TThread 不会完成它的工作!
我创建了一个 TThread 类来执行一些套接字操作,问题是,除非我向其中添加 MessageBox,否则代码无法工作,除非我在其之前放置 MessageBox 调用,否则套接字将无法工作
Sleep(2000); //Waiting for the Socket to Come to the Array
// Messagebox(0, '', '', 0); { Wont work unless this line is Uncommented }
if Server.ClientList[Handle] <> nil then
begin
if (Server.ClientList[Handle].Connected) and (AppSocket.Connected) do
begin
// Send Data on Socket
// Relay Data between Server.ClientList[Handle] and AppSocket;
end;
i created a class of TThread to do some socket operations, the thing is, the code doesnt work unless i add MessageBox to it, sockets wont work unless i put a MessageBox call before it
Sleep(2000); //Waiting for the Socket to Come to the Array
// Messagebox(0, '', '', 0); { Wont work unless this line is Uncommented }
if Server.ClientList[Handle] <> nil then
begin
if (Server.ClientList[Handle].Connected) and (AppSocket.Connected) do
begin
// Send Data on Socket
// Relay Data between Server.ClientList[Handle] and AppSocket;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用非阻塞套接字,那么您的线程需要一个正在运行的消息队列和处理循环。这就是调用
MessageBox()
起作用的原因 - 它是一个模式对话框,在内部泵送调用线程的消息队列。您的线程需要在连接的生命周期内循环调用PeekMessage()
或GetMessage()
。您的循环可以使用MsgWaitForMultipleObjects()
来检测消息队列何时有需要处理的内容以及您的线程是否还有其他需要执行的操作。Assuming you are using non-blocking sockets, then your thread needs a running message queue and processing loop. That is why calling
MessageBox()
works - it is a modal dialog that pumps the calling thread's message queue internally. Your thread needs to callPeekMessage()
orGetMessage()
in a loop for the lifetime of the connection(s). Your loop can useMsgWaitForMultipleObjects()
to detect when the message queue has something to process, if your thread has other things it needs to do.尝试用 Application.ProcessMessages 替换 Messagebox() 并看看会发生什么。
Try to replace Messagebox() with Application.ProcessMessages and see what happens.