I/O限制异步操作
2013-11-13 18:30
459 查看
CLR非异步操作读取文件的过程图
非异步操作主要是因为每次请求硬件如(硬盘,网卡等)的线程都处于阻塞状态,导致以后的请求都需要重新创建新的线程。导致线程上下文的切换频繁。
异步IO操作主要是通过每次的请求完硬件创造好IRP,线程就会返回到线程池中,将来硬件完成任务时,会主动去线程池中找线程来继续完成的操作。这样返回到线程池的线程就可以去做自己的事情了。
可以使用一个去本市旅游乘车的例子来说明:
可以请一班车,送到旅游地方,车该去哪里去哪里,回去的时间给公交公司打电话,来一辆车,这样不用让来的那个车在旅游地点一直等着,造成资源的浪费。
下面是使用IO异步操作的一个实例
注意end****方法都是出现在Begin****中的回调函数中。
非异步操作主要是因为每次请求硬件如(硬盘,网卡等)的线程都处于阻塞状态,导致以后的请求都需要重新创建新的线程。导致线程上下文的切换频繁。
异步IO操作主要是通过每次的请求完硬件创造好IRP,线程就会返回到线程池中,将来硬件完成任务时,会主动去线程池中找线程来继续完成的操作。这样返回到线程池的线程就可以去做自己的事情了。
可以使用一个去本市旅游乘车的例子来说明:
可以请一班车,送到旅游地方,车该去哪里去哪里,回去的时间给公交公司打电话,来一辆车,这样不用让来的那个车在旅游地点一直等着,造成资源的浪费。
下面是使用IO异步操作的一个实例
{ m_pipe.BeginRead(data, 0, data.Length, GotRequest, data); } private void GotRequest(IAsyncResult result) { // The client sent us a request, process it. Int32 bytesRead = m_pipe.EndRead(result); Byte[] data = (Byte[])result.AsyncState; // My sample server just changes all the characters to uppercase // But, you can replace this code with any compute-bound operation data = Encoding.UTF8.GetBytes( Encoding.UTF8.GetString(data, 0, bytesRead).ToUpper().ToCharArray()); // Asynchronously send the response back to the client m_pipe.BeginWrite(data, 0, data.Length, WriteDone, null); }
注意end****方法都是出现在Begin****中的回调函数中。
IO异步操作异常处理
一般只需要在End里面去捕捉就ok了。下面演示一个例子public static void Go() { WebRequest webRequest = WebRequest.Create("http://0.0.0.0/"); webRequest.BeginGetResponse(ProcessWebResponse, webRequest); Console.ReadLine(); } private static void ProcessWebResponse(IAsyncResult result) { WebRequest webRequest = (WebRequest)result.AsyncState; WebResponse webResponse = null; try { webResponse = webRequest.EndGetResponse(result); Console.WriteLine("Content length: " + webResponse.ContentLength); } catch (WebException we) { Console.WriteLine(we.GetType() + ": " + we.Message); } finally { if (webResponse != null) webResponse.Close(); } }
相关文章推荐
- Clr Via C#读书笔记---计算限制的异步操作
- 【C#】C#线程_计算限制的异步操作
- 线程也疯狂------计算限制的异步操作
- Clr Via C#读书笔记---I/O限制的异步操作
- 《CLR via C#》读书笔记 之 计算限制的异步操作
- 【C#进阶系列】26 计算限制的异步操作
- 第二十六章 计算限制的异步操作
- CLR Via CSharp读书笔记(26) - 计算限制的异步操作
- CLR Via CSharp读书笔记(27) - IO限制的异步操作
- CLR_via_C#.3rd 翻译[25.7 使用专用线程执行异步的计算限制操作]
- [CLR via C#]26. 计算限制的异步操作
- 【C#进阶系列】27 I/O限制的异步操作
- 《CLR via C#:框架设计》读书笔记 - 计算限制的异步操作
- 计算限制的异步操作
- C# 使用APM执行受I/O限制的异步操作(转)
- [CLR via C#]26. 计算限制的异步操作
- Task:取消异步计算限制操作 & 捕获任务中的异常
- PHP字符操作之限制新闻标题的显示字数
- jquery下ajax异步执行操作笔记
- 使用BackgroundWorker组件进行异步操作编程[转]