2014-08-27 84 views
1

我试图用异步/等待和使用ContinueWith()重新编写一些旧代码并检查TaskContinuationOptions.NotOnFaulted的异常。Task.ContinueWith()未按预期方式执行

当我调试代码时,我注意到它没有像我期望的那样运行。 两个webrequests都成功,但只有第一个连续处理响应。

第二延续未完成 ,最后一个给我的结果:

Id = 1, Status = RanToCompletion, Method = "{null}", Result = "System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]" 

和结果:

Id = 2, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" 

我的问题是什么我做错了,我能这样做第二次继续完成。 如果使用ContinueWith将任务链接在一起被认为是一种很好的做法,或者如果有更好的方法可以在不编写一堆笨拙的方法的情况下做到这一点,我也会感兴趣吗?说 感谢您的帮助

using Newtonsoft.Json.Linq;     

    var api = new Api(); 
    var order = new Dictionary<string, object>(); 
    await api.MakeRequest(Api.Endpoint.Orders, HttpMethod.Get, null, "?completed=false&page=" + count) 
    //Look for new Orders 
    .ContinueWith(ant => 
    { 
      dynamic jsonOrder = JObject.Parse(ant.Result); 
      JArray data = jsonOrder.data; 
      //Process Json Response 
      order.Add("customer_name", (string)data[j]["customer_name"]); 
      order.Add("product_id", (string)data[j]["product_id"]); 
      order.Add("order_id", (string)data[j]["order_id"]); 
      order.Add("timestamp", (int)data[j]["timestamp"]); 
      //Entries are successfully added 
    }, TaskContinuationOptions.NotOnFaulted) 
    //Now get more details about the product 
    .ContinueWith(async (ant) => 
    { 
      string result = await api.MakeRequest(Api.Endpoint.Product, HttpMethod.Get, null, (string)order["product_id"]); 
      //The Request succeeds 

      //This code block does not execute 
      dynamic json = JObject.Parse(result); 
      order.Add("deadline", (int)json.data.deadline); 
      order.Add("price", (string)json.data.price); 
      order.Add("amount", (int)json.data.amount); 
      //This code block does not execute 

    }, TaskContinuationOptions.NotOnFaulted) 
    //Get some more details about the Customer (isRecurring? etc) 
    .ContinueWith(async (ant) => 
    { 
     //Some more code here 
    } 
+1

*如果有更好的方式来做到这一点,而无需编写一串笨拙的方法是什么?*使用异步/ AWAIT – 2014-08-27 09:14:30

+1

你并不需要ContinueWith你正在等待一个任务,那就是整个的语法点。只需等待任务,然后等待后的代码将只在任务完成或失败时执行。您从您正在等待的方法返回的值中提取结果,例如var result = await api.MakeRequest(...'。你可以使用标准的try/catch语法来处理异常。 – 2014-08-27 09:24:00

+0

是的,这是有道理的。但是如果我想并行处理并不等待任务呢? – Timo1995 2014-08-27 09:49:51

回答

1

像@Ben罗宾逊利用await自动注册方法作为如果操作成功时,才会执行一个延续的休息,否则会抛出异常。如果在异步操作完成后您不需要返回到当前的SynchrionizationContext,即,该方法的其余部分将继续在线程池线程上执行,我会更改我的方法以删除ContinueWith调用并考虑使用ConfigureAwait(false)。您也可能会发现this article有用。

var api = new Api(); 
var order = new Dictionary<string, object>(); 

await api.MakeRequest(Api.Endpoint.Orders, HttpMethod.Get, null, "?completed=false&page=" + count).ConfiugureAwait(false); 

//Look for new Orders 
dynamic jsonOrder = JObject.Parse(ant.Result); 
JArray data = jsonOrder.data; 
//Process Json Response 
order.Add("customer_name", (string)data[j]["customer_name"]); 
order.Add("product_id", (string)data[j]["product_id"]); 
order.Add("order_id", (string)data[j]["order_id"]); 
order.Add("timestamp", (int)data[j]["timestamp"]); 

//Now get more details about the product 
string result = await api.MakeRequest(Api.Endpoint.Product, HttpMethod.Get, null, (string)order["product_id"]).ConfiugureAwait(false); 

dynamic json = JObject.Parse(result); 
order.Add("deadline", (int)json.data.deadline); 
order.Add("price", (string)json.data.price); 
order.Add("amount", (int)json.data.amount); 
+0

但是为什么代码在第二个之后没有等待?继续执行? – tonlika 2018-03-10 10:48:42