开发者

What can happen when I forcly terminate thread at Winsock connect function?

What can happen when I use TerminateThread while Winsock connect function is in progress? I need to immediately shutdown the thread which is currently connecting to the socket b开发者_Go百科ut I don't know what can happen to the network adapter.

The connect function has unfortunately no timeout parameter and the default system timeout value is too high. So is there any risk to interrupt this API function?

I've used Delphi tag just because I'm writing my application in Delphi, but it's much more Winsock and Windows API question.

Thanks


The only way to abort a connect() call is to close the socket from another thread context. Otherwise, use ConnectEx() with overlapped I/O, like Martin said.


By calling TerminateThread you create huge memory and resource leak. It may be OK if you do this just before your program exits (though it is not nice anyway...). But if you do this several times during program execution, your program finally crashes - no memory. Network adapter and its driver will be OK, they are protected well from buggy user mode programs.


Use ConnectEx() in an overlapped call? You could either use an event object, (a TEvent, say), in hEvent and wait on both that and some terminator event with WaitForMultipleObjects() or use a completion routine and wait on the terminator with WaitForSingleObjectEx() in a loop,that ignores WSA_OUTPUT_PENDING and IO_COMPLETION as results.

I guess it would be easier in this case to just wait with WaitForMultipleObjects().

Rgds, Martin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜