用到UdpClient的一点经验

Thread.Abort对UdpClient.Receive阻塞的线程无效

http://computer-programming-forum.com/4-csharp/184f9d4ee63704fc.htm

This is not a bug (just very poorly documented :) 

Thread.Abort throws an exception only when the thread is in managed code. 
Since calling UdpClient.Receive call a blocking function of the WinSock API 
it will not receive the abort exception. Only after UdpClient.Receive 
finishes (hence you are back in managed code) the Thread.Abort method 
re-throws the exception. Lookup in the help files at Thread.Abort(). 

How to solve this problem? 
Use a Socket class (instead of UdpClient) and use the asynchronously methods 
BeginReceiveFrom and EndReceiveFrom (of course also for XXXSendTo). These 
methods are a litter harder to use but your thread now remains in managed 
code while waiting for the receive operation (still unmanaged code) to 
complete. 

-- 
Sander Leer 

 

Quote:
> If a thread is waiting on a call to UdpClient.Receive, there is no way to 
> return from this call unless the UdpClient is closed or data is received. 
> Calling Thread.Abort on the blocked thread has no effect. 

> The attached code illustrates the problem. 

> Is this a bug? 

> DAve 

 

封装Socket.BeginReceive/EndReceive以支持Timeout

http://www.cnblogs.com/dytes/archive/2012/08/13/SocketAsyncOpWithTimeout.html

posted on 2015-09-29 17:42  sPhinX  阅读(353)  评论(0编辑  收藏  举报

导航