没有 TaskCompletionSource 的任务链?
我正在将一些异步/等待代码转换为链接任务,以便我可以在已发布的框架中使用它。等待代码如下所示
public async Task<TraumMessage> Get() {
var message = await Invoke("GET");
var memorized = await message.Memorize();
return memorized;
}
,
Task<TraumMessage> Invoke(string verb) {}
Task<TraumMessage> Memorize() {}
我希望链接 Invoke
和 Memorize
以返回由 Memorize
生成的任务,但这会导致 <代码>任务<任务TaskCompletionSource
作为我的信号:
public Task<TraumMessage> Get() {
var completion = new TaskCompletionSource<TraumMessage>();
Invoke("GET").ContinueWith( t1 => {
if(t1.IsFaulted) {
completion.SetException(t1.Exception);
return;
}
t1.Result.Memorize().ContinueWith( t2 => {
if(t2.IsFaulted) {
completion.SetException(t2.Exception);
return;
}
completion.SetResult(t2.Result);
});
});
return completion.Task;
}
有没有办法在没有 TaskCompletionSource
的情况下完成此任务?
I'm converting some async/await code to chained tasks, so I can use it in the released framework. The await code looks like this
public async Task<TraumMessage> Get() {
var message = await Invoke("GET");
var memorized = await message.Memorize();
return memorized;
}
where
Task<TraumMessage> Invoke(string verb) {}
Task<TraumMessage> Memorize() {}
I was hoping to chain Invoke
and Memorize
to return the task produced by Memorize
, but that results in a Task<Task<TraumMessage>
. The solution i've ended up is a TaskCompletionSource<TraumMessage>
as my signal:
public Task<TraumMessage> Get() {
var completion = new TaskCompletionSource<TraumMessage>();
Invoke("GET").ContinueWith( t1 => {
if(t1.IsFaulted) {
completion.SetException(t1.Exception);
return;
}
t1.Result.Memorize().ContinueWith( t2 => {
if(t2.IsFaulted) {
completion.SetException(t2.Exception);
return;
}
completion.SetResult(t2.Result);
});
});
return completion.Task;
}
Is there a way to accomplish this without the TaskCompletionSource
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,该框架附带了一个方便的 Unwrap() 扩展方法,可以满足您的需求。
如果您要取消,那么显然您需要将取消令牌传递到适当的位置。
Yes, the framework comes with a handy Unwrap() extension method for exactly what you want.
If you're doing cancellation then you'll need to pass cancel tokens into the appropriate places, obviously.
我认为这几乎是实现你想要的目标的唯一方法。延续 API 不支持将不同的任务链接在一起,因此您必须使用
TaskCompletionSource
就像协调工作一样。我没有在这台机器上安装 Async CTP,但是为什么你不使用反编译器(或者 ILDASM,如果你知道如何阅读 IL)来查看代码,看看它在做什么。我敢打赌它在幕后所做的事情与你的 TCS 代码非常相似。
I think that's pretty much the only way to accomplish what you want. Chaining disparate Tasks together isn't supported by the continuation APIs, so you have to resort to using a
TaskCompletionSource
like you have to coordinate the work.I don't have the Async CTP installed on this machine, but why don't you take a look at the code with a decompiler (or ILDASM if you know how to read IL) to see what it's doing. I bet it does something very similar to your TCS code under the covers.
您可以使用附加的子任务。只有当所有子任务都完成时,父任务才会转换为已完成状态。异常会传播到父任务。
您将需要一个结果持有者,因为结果将在父任务的委托完成后分配,但会在父任务延续运行时设置。
像这样:
You can use attached child tasks. The parent task will only transition into the completed status when all child tasks are complete. Exceptions are propagated to the parent task.
You will need a result holder, as the result will be assigned after the parent task's delegate has finished, but will be set when the parent tasks continuations are run.
Like this: