I/O限制异步操作

CLR非异步操作读取文件的过程图

image

非异步操作主要是因为每次请求硬件如(硬盘,网卡等)的线程都处于阻塞状态,导致以后的请求都需要重新创建新的线程。导致线程上下文的切换频繁。

异步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();
        }
    }
posted @ 2013-11-13 18:30  haiziguo  阅读(414)  评论(0编辑  收藏  举报