gpt4 book ai didi

c# - Task.ContinueWith() 未按预期执行

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:35 26 4
gpt4 key购买 nike

我正在尝试使用异步/等待重写我的一些旧代码,并使用 ContinueWith() 链接任务并使用 TaskContinuationOptions.NotOnFaulted 检查异常。

当我调试代码时,我注意到它没有按预期运行。两个网络请求都成功,但只有第一个继续处理响应。

第二次继续未完成最后一个给了我结果:

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
}

最佳答案

就像@Ben Robinson 所说的那样,await 的使用会自动将方法的其余部分注册为继续,只有在操作成功时才​​会执行,否则会抛出异常。我会更改我的方法以删除 ContinueWith 调用并考虑使用 ConfigureAwait(false) 如果您不需要返回当前 SynchrionizationContext 异步操作完成后,即该方法的其余部分将继续在线程池线程上执行。您还可以找到 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);

关于c# - Task.ContinueWith() 未按预期执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com