2011-12-15 75 views
4

我试图找出等待一些I/O完成端口完成的最佳方法。c#并行IO完成端口

对于这种情况,假设我在一个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?我正在使用的数据服务一次只能获得一条记录(这超出了我的控制范围),我希望这些单独的查询能够并行运行。

回答

0

使用TPL很容易。

public ActionResult Index(IEnumerable<int> ids) 
{ 
    var result = ids.AsParallel() 
     .Select(id => GetOrder(id)) 
     .ToList(); 
    return Json(result); 
} 

Order GetOrder(int id) { ... } 
+0

这将运行的线程数量的一些,将仍然阻止IIS线程。 – 2012-05-01 16:36:27

2

这是我已经结束了使用运行批处理操作异步里面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 
    } 
+0

您是否在上下文的连接字符串上启用了[`AsynchronousProcessing`](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnectionstringbuilder.asynchronousprocessing.aspx)? – 2012-05-01 16:53:39