加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#并行IO完成端口

发布时间:2020-12-15 17:22:34 所属栏目:百科 来源:网络整理
导读:我正在试图找出等待一些I / O完成端口完成的最佳方法. 对于这种情况,让我们说我在一个MVC3网络应用程序中. (我的理解是建议使用I / O完成端口,因此我可以将原始线程返回给IIS来为其他请求提供服务) 假设我有一系列ID,我想从某个网络调用中获取每个ID的对象.
我正在试图找出等待一些I / O完成端口完成的最佳方法.

对于这种情况,让我们说我在一个MVC3网络应用程序中. (我的理解是建议使用I / O完成端口,因此我可以将原始线程返回给IIS来为其他请求提供服务)

假设我有一系列ID,我想从某个网络调用中获取每个ID的对象.

并行化这种同步方法的最佳方法是什么?

public class MyController: Controller
   {
       public ActionResult Index(IEnumerable<int> ids)
       {
            ids.Select(id => _context.CreateQuery<Order>("Orders")
                                     .First(o => o.id == id));
            DataServiceQuery<Order> query = _context.CreateQuery<Order>("Orders");
            return Json(query);
       }

       private DataServiceContext _context; //let's ignore how this would be populated
   }

我知道它会像这样开始:

public class MyController: AsyncController
   {
       public void IndexAsync(IEnumerable<int> ids)
       {
            // magic here...

            AsyncManager.Sync(() => AsyncManager.Parameters["orders"] = orders);
       }

       public ActionResult IndexCompleted(IEnumerable<Order> orders)
       {
            return Json(orders);
       }

       private DataServiceContext _context; //let's ignore how this would be populated
   }

我应该使用DataServiceContext.BeginExecute方法吗? DataServiceContext.BeginExecuteBatch?我正在使用的数据服务一次只能获得一条记录(这超出了我的控制范围),我希望这些单独的查询并行运行.

解决方法

这是我最终用于在MVC3中运行一批异步操作的模式:

public class MyController: AsyncController
   {
       public void IndexAsync(int[] ids)
       {
            var orders = new Orders[ids.Length];
            AsyncManager.Parameters["orders"] = orders;

            // tell the async manager there are X operations it needs to wait for
            AsyncManager.OutstandingOperations.Increment(ids.Length);

            for (int i = 0; i < ids.Length; i++){
               var index = i; //<-- make sure we capture the value of i for the closure

               // create the query
               var query = _context.CreateQuery<Order>("Orders");

               // run the operation async,supplying a completion routine
               query.BeginExecute(ar => {
                   try {
                       orders[index] = query.EndExecute(ar).First(o => o.id == ids[index]);
                   }
                   catch (Exception ex){
                       // make sure we send the exception to the controller (in case we want to handle it)
                       AsyncManager.Sync(() => AsyncManager.Parameters["exception"] = ex);
                   }
                   // one more query has completed
                   AsyncManager.OutstandingOperations.Decrement();
               },null);
            }
       }

       public ActionResult IndexCompleted(Order[] orders,Exception exception)
       {
            if (exception != null){
                throw exception; // or whatever else you might like to do (log,etc)
            }
            return Json(orders);
       }

       private DataServiceContext _context; //let's ignore how this would be populated
   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读