简体   繁体   English

C#中的异步是否与F#中的异步相同?

[英]Is asynchronous in C# the same implementation as in F#?

C#4.5中的异步实现与线程使用方式中的F#2完全相同吗?

They are different. 它们是不同的。 The main difference is that C# uses standard .NET Task<T> to represent asynchronous computations while F# uses its own type called Async<T> . 主要区别在于C#使用标准.NET Task<T>来表示异步计算,而F#使用自己的类型Async<T>

More specifically, the key differences are: 更具体地说,关键的区别是:

  • AC# async method creates a Task<T> that is immediately started ( hot task model) while F# creates a computation that you have to start explicitly ( generator model). AC#async方法创建一个立即启动的Task<T>热任务模型),而F#创建一个必须明确启动的计算( 生成器模型)。 This means that F# computations are easier to compose (you can write higher level abstractions). 这意味着F#计算更容易编写(您可以编写更高级别的抽象)。

  • In F# you also get better control over how is the computation started. 在F#中,您还可以更好地控制计算的开始方式。 You can start a computation using Async.Start to start it in the background or Async.StartImmediate to start it on the current thread. 您可以使用开始计算Async.Start启动它在后台或Async.StartImmediate启动它在当前线程上。

  • F# asynchronous workflows support cancellation automatically, so you do not have to pass CancellationToken around. F#异步工作流程支持自动取消 ,因此您不必通过CancellationToken

  • Perhaps another consequence of the first point is that F# async workflows also support tail-recursion, so you can write recursive workflows (this would not work easily in C#, but C# does not use this programming style) 第一点的另一个结果可能是F#异步工作流也支持尾递归,因此您可以编写递归工作流(这在C#中不起作用,但C#不使用此编程样式)

I wrote a more detailed article about this topic: Asynchronous C# and F# (II.): How do they differ? 我写了一篇关于这个主题的更详细的文章: 异步C#和F#(II。):它们有什么不同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM