I/O限制异步操做

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

image

非异步操做主要是由于每次请求硬件如(硬盘,网卡等)的线程都处于阻塞状态,致使之后的请求都须要从新建立新的线程。致使线程上下文的切换频繁。异步

异步IO操做主要是经过每次的请求完硬件创造好IRP,线程就会返回到线程池中,未来硬件完成任务时,会主动去线程池中找线程来继续完成的操做。这样返回到线程池的线程就能够去作本身的事情了。函数

能够使用一个去本市旅游乘车的例子来讲明:this

能够请一班车,送到旅游地方,车该去哪里去哪里,回去的时间给公交公司打电话,来一辆车,这样不用让来的那个车在旅游地点一直等着,形成资源的浪费。spa

下面是使用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****中的回调函数中。code

IO异步操做异常处理

通常只须要在End里面去捕捉就ok了。下面演示一个例子server

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();
        }
    }
相关文章
相关标签/搜索