码迷,mamicode.com
首页 > 其他好文 > 详细

用到UdpClient的一点经验

时间:2015-09-29 18:41:03      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

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 

用到UdpClient的一点经验

标签:

原文地址:http://www.cnblogs.com/s5689412/p/4846908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!