本文介绍了Task.Factory.FromAsync和BeginX / EndX之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有非常相似的code。使用从TcpClient的标准的BeginRead和EndRead方法和使用Task.Factory.FromAsync时。

I have very similar code when using the standard BeginRead and EndRead methods from the TcpClient and using Task.Factory.FromAsync.

下面是一些例子..错误处理code未显示。

Here are some examples.. Error handling code not shown.

Task.Factory.FromAsync:

private void Read(State state)
{
    Task<int> read = Task<int>.Factory.FromAsync(state.Stream.BeginRead, state.Stream.EndRead, state.Bytes, state.BytesRead, state.Bytes.Length - state.BytesRead, state, TaskCreationOptions.AttachedToParent);

    read.ContinueWith(FinishRead);
}

private void FinishRead(Task<int> read)
{
    State state = (State)read.AsyncState;

    state.BytesRead += read.Result;
}

标准使用回调与BeginRead的和EndRead的:

private void Read(State state)
{
    client.BeginRead(state.Bytes, state.BytesRead, state.Bytes.Length - state.Bytes.Read, FinishRead, state);
}

private void FinishRead(IAsyncResult async)
{
    State state = (State)async.AsyncState;

    state.BytesRead += state.Stream.EndRead(async);
}

这两个工作很好,但我很好奇他们之间的分歧。的code表示两者的线是pretty的多等效,它们似乎都执行完全相同的功能,并具有相同的效率。哪一个是preferable?你会在生产中code,而见?

Both of these work fine but I am curious of their differences. The lines of code for both are pretty much equivalent and they both seem to perform the exact same function and have the same efficiency. Which one is preferable? What would you rather see in production code?

推荐答案

我宁愿看到任务&LT; T&GT; 基于code:

I would much rather see Task<T> based code:

  • 它提供了成分更轻松;例如,它是相当容易写一个方法,需要的收集任务&LT; T&GT; 任务,并返回一个重新$ P $另一个任务psents这些任务的多数裁决。同样,你可以等待,直到任务集合中的任何一个已完成等。
  • 在它提供的地方继续运行的更加灵活的调度。
  • 在它允许比自己同类型安全返回的任务和更大量的信息,有点贫血的IAsyncResult 类型的的BeginRead
  • 这是简单的指定错误处理和消除与比使用的开始/结束模式的任务。
  • 任务&LT; T&GT; 越来越在C#5与异步/计谋更好的语言支持 - 如果你的codeBase的已使用任务&LT; T&GT ; 普遍地,这将是的的更容易利用这一点
  • It provides for composition more easily; for example, it's reasonably easy to write a method which takes a collection of Task<T> tasks and returns another task which represents the majority verdict of those tasks. Likewise you can wait until any one of a collection of tasks has completed, etc.
  • It provides more flexible scheduling of where the continuation is run.
  • It allows the task itself to be returned with type safety and a lot more information than the somewhat anaemic IAsyncResult type returned by BeginRead.
  • It's simpler to specify error handling and cancellation with tasks than using the Begin/End model.
  • Task<T> is getting better language support in C# 5 with async/await - if your codebase already uses Task<T> pervasively, it'll be much easier to take advantage of this

基本上在现代code运行在.NET 4中,任务&LT; T&GT; 是重$ P $的惯用方式psenting一个持续的任务。这是一个更丰富的环境比以前的尝试工作,我会接受它,如果你有机会。显然,如果您使用的是.NET 3.5或更早版本,生活是有点困难,但我假设,当你问这个问题,任务&LT; T&GT; 是一个选项...

Basically in modern code running on .NET 4, Task<T> is the idiomatic way of representing an on-going task. It's a much richer environment to work in than earlier attempts, and I would embrace it if you have the chance. Obviously if you're using .NET 3.5 or earlier, life is a bit harder, but I'm assuming that as you're asking the question, Task<T> is an option...

这篇关于Task.Factory.FromAsync和BeginX / EndX之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:36