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

用到UdpClient的一点经验的更多相关文章

  1. 使用VS2010开发Qt程序的一点经验

    导读 相比于Qt Creator,我更喜欢用VS2010来进行开发.虽然启动时间相对较慢,但是VS下强大的快捷键和丰富的插件,以及使用多年的经验,都让我觉得在开发过程中得心应手.其中最重要的一点是,有 ...

  2. 使用VS2010开发Qt程序的一点经验(转载)

    转载:http://www.cnblogs.com/csuftzzk/p/VS_Qt_Experience.html 导读 相比于Qt Creator,我更喜欢用VS2010来进行开发.虽然启动时间相 ...

  3. 关于删除 hao123 主页设置的一点经验

    :first-child { margin-top: 0px; } blockquote>:last-child { margin-bottom: 0px; } --> 说一说关于删除 h ...

  4. 计算机顶级会议Rankings && 英文投稿的一点经验

    英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...

  5. Python爬虫脚本 ,Uni-APP复选框做出双向绑定 ,Net5工作流建模 。的一点经验

    从业C#开发多年,现在也经常用到Python 做网络爬虫 ,用Uni-app做手机前端.攒了一点经验.供其他多语言开发程序员借鉴吧. Python做爬虫和其他的方式做爬虫最大的区别应该在于. Pyth ...

  6. 封装RabbitMQ.NET Library 的一点经验总结

    这篇文章内容会很短,主要是想给大家分享下我最近在做一个简单的rabbitmq客户端类库的封装的经验总结,说是简单其实一点都不简单.为了节省时间我主要按照Library的执行顺序来介绍,在你看来这里仅仅 ...

  7. 关于访问MSMQ远端私有队列的一点经验

    这里应该将私有队列称做"专用队列"好像更贴切一些了,O(∩_∩)O 可以访问远程主机的MSMQ的私有队列的,这个是毋庸置疑的,但需要说明的是不能通过代码创建私有队列,关于这一点,我 ...

  8. 封装RabbitMQ.NET Library 的一点经验总结 转载

    这篇文章内容会很短,主要是想给大家分享下我最近在做一个简单的rabbitmq客户端类库的封装的经验总结,说是简单其实一点都不简单.为了节省时间我主要按照Library的执行顺序来介绍,在你看来这里仅仅 ...

  9. 关于Listview布局的一点经验

    1.尽量是给item一个固定高度,最外层不要设高度,里面套一层设置一个固定高度:如果用wrap_content的话,之后用alignTop等会出奇怪的问题. 2.如果要使用alignTop align ...

随机推荐

  1. js随机生成[n,m)的数字(不包括m)

    Math.random();//随机生成0到1的数字 Math.floor();//取小整 Math.floor(Math.random()*(最大值 - 最小值) + 最小值) 生成2到8的数:Ma ...

  2. JS图片加载失败用默认图片代替

    1.onerror 事件会在文档或图像加载过程中发生错误时被触发. 当图片不存在时,将触发onerror,onerror 中img为 指定的默认图片. 图片存在则显示正常图片,图片不存在将显示默认. ...

  3. 如何将一个Maven项目转化成一个Eclipse项目

    有时候我们需要将一个Maven项目导入到Eclipse中,直接作为一个普通的eclipse项目来导入是不行的,我们可以通过一个命令来实现:mvn eclipse:eclipse 1. 进入该Maven ...

  4. 跨域的问题(jsonp和cors)

    由于浏览器的同源策略,用户想要跨域访问浏览器就会报错,那么就涉及到解决跨域的问题.最近我接触到的解决方法是两个,jsonp和cors. jsonp(json with padding)我们虽然不能直接 ...

  5. VSCode个人实用插件

    1.汉化插件 Chinese (Simplified) Language Pack for Visual Studio Code 发布者:Microsoft 2.主题插件(IDEA主题) Darcul ...

  6. Silverlight FullScreen 全屏

    <UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...

  7. Java基础15-数组实例学生管理系统

    import java.util.Scanner; public class Student{ public static void main(String[] args){ Scanner in=n ...

  8. httpd编译安装php

    wget http://hk1.php.net/distributions/php-5.6.31.tar.gz yum groupinstall "Development Tools&quo ...

  9. poj 1947 树形背包

    重做这道题 http://blog.csdn.net/woshi250hua/article/details/7632785 http://blog.csdn.net/shuangde800/arti ...

  10. jQuery属性选择器中加变量

    $(function () { $('#bkhandle').on('click','#bkdel',function () { $.ajax( { url:"{% url 'bkdel' ...