异步套接字阅读:发起线程一定不能退出 - 怎么办? [英] Asynchronous socket reading: the initiating thread must not be exited - what to do?

查看:312
本文介绍了异步套接字阅读:发起线程一定不能退出 - 怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的NetworkStream我读异步(使用异步/等待)

I have a NetworkStream which I read asynchronously (using async/await)

await Task<int>.Factory.FromAsync((cb, state) => stream.BeginRead(buffer, offset, readLen - offset), stream.EndRead, null);

不幸的是,一个IO异常,有时会出现:由于线程退出或应用程序请求的I / O操作已中止。

Unfortunatly, an io exception sometimes occurs: "The I/O operation has been aborted because of either a thread exit or an application request."

我相信我打Socke.EndReceive文件的要求:的http:// msdn.microsoft.com/en-us/library/w7wtt64b.aspx 。其中规定:

I believe I hit a requirement documented in Socke.EndReceive: http://msdn.microsoft.com/en-us/library/w7wtt64b.aspx . Which states:

所有I / O由给定线程启动被取消时,该线程
  退出。未处理的异步操作可能会失败,如果线程退出
  操作完成之前。

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

由于异步方法上的默认调度程序运行,这种要求不能得到保证。

Because the async method runs on the default scheduler, this requirement cannot be assured.

有没有办法解决?我是否需要启动一个专用的线程来启动I / O?

Is there a way around this? Do I need to start a dedicated Thread to initiate I/O?

最好的问候,德克

推荐答案

我也问过的parallelextensions论坛同样的问题:

I have asked the same question on the parallelextensions forum:

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/d116f7b0-c8ee-4ce4-a9b8-4c38120e45a4\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/d116f7b0-c8ee-4ce4-a9b8-4c38120e45a4

基本上,线程池不会停止有异步I / O挂起任何线程。所以,只要一开始就应该不会出现这个问题的线程池的套接字操作。

Basically, the ThreadPool will not stop any threads that has asynchronous I/O pending. So as long as one starts the socket operation on the ThreadPool this problem should not occur.

这篇关于异步套接字阅读:发起线程一定不能退出 - 怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆