zoukankan      html  css  js  c++  java
  • .net framework 4.5 异步初体验(实习)

    良好的用户体验对于软件的成功越来越重要,当然不仅仅是软件(比如苹果)。到底什么样的软件才能让人用着舒服呢?

    排除网络,硬件,只从软件本身角度来说,最重要的也许就是及时响应了。ui层我们可以用JavaScript的Ajax,后台可以

    多线程。现在有很多成熟的JavaScript脚本库已经封装好了Ajax的异步方法,我们用起来相对还是比较方便的,但在过去

    几年里要是在后台想使用异步总是特别的小心,不好测,不好调,还有未知,个人来说,能不用就不用。。。

    异步是什么呢?打个比方说,网页上有一个按钮(button),这个按钮绑定的事件(click_event)是要到数据库中取大量的数据,当

    button的click事件被触发时,如果是同步,则在数据库返回数据前,我们会失去UI的操作权,nothing to do but wait;而异步

    则是,我告诉程序,你去数据库取数据,至于你什么时候取回来我不管,我可以继续做我的事儿,只要完成后告诉我一声就行

    了。

    下载举个小例子,一个CopyTo方法

    4.5以前要实现异步

    public static IAsyncResult BeginCopyTo(Stream source, Stream destination)
    {
      var tcs = new TaskCompletionSource();
     
      byte[] buffer = new byte[0x1000];
      Action<IAsyncResult> readWriteLoop = null;
      readWriteLoop = iar =>
      {
        try
        {
          for (bool isRead = iar == null; ; isRead = !isRead)
          {
            switch (isRead)
            {
              case true:
                iar = source.BeginRead(buffer, 0, buffer.Length, readResult =>
                {
                  if (readResult.CompletedSynchronously) return;
                  readWriteLoop(readResult);
                }, null);
                if (!iar.CompletedSynchronously) return;
                break;
     
              case false:
                int numRead = source.EndRead(iar);
                if (numRead == 0)
                {
                  tcs.TrySetResult(true);
                  return;
                }
                iar = destination.BeginWrite(buffer, 0, numRead, writeResult =>
                {
                  try
                  {
                    if (writeResult.CompletedSynchronously) return;
                    destination.EndWrite(writeResult);
                    readWriteLoop(null);
                  }
                  catch(Exception e) { tcs.TrySetException(e); }
                }, null);
                if (!iar.CompletedSynchronously) return;
                destination.EndWrite(iar);
                break;
              }
            }
          }
        }
        catch(Exception e) { tcs.TrySetException(e); }
      };
      readWriteLoop(null);
     
      return tcs.Task;
    }
     
    public static void EndCopyTo(IAsyncResult asyncResult)
    {
      ((Task)asyncResult).Wait();
    }
     

    4.5中

    public static async void CopyToAsync(Stream source, Stream destination)
    {
      byte[] buffer = new byte[0x1000];
      int numRead;
      while((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) > 0)
      {
        await destination.WriteAsync(buffer, 0, numRead);
      }
    }

    是不是太简单了,就是在同步方法里加上几个关键字async,await

  • 相关阅读:
    备份
    >> 与 > >
    为什么需要htons(), ntohl(), ntohs(),htons() 函数
    小技巧
    C++头文件
    宏定义中的#,##操作符和... and _ _VA_ARGS_ _与自定义调试信息的输出
    OpenCV摄像头简单程序
    [转]让Linux的tty界面支持中文
    opencv 2 computer vision application programming第四章翻译
    OpenCV条码(6)简单实现
  • 原文地址:https://www.cnblogs.com/wangcl/p/2567262.html
Copyright © 2011-2022 走看看