简体   繁体   English

如何在F#中编写此异步IO C#代码?

[英]How do I write this async IO C# code in F#?

I have this code: 我有这个代码:

public async Task CreateFileAsync(string filePath, byte[] bytes)
    {
        using (var sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate))
        {
            sourceStream.Seek(0, SeekOrigin.End);
            await sourceStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
        }
    }

I want to write it on F#, I get as far as this before I can't figure out what to do: 我想在F#上写它,在我无法弄清楚该怎么做之前我得到了这个:

module myModule
open System.IO;
let CreateFileAsync (filePath: string, bytes : byte[]) =
    use sourceStream = File.Open(filePath, FileMode.OpenOrCreate)
        |> sourceStream.Seek(0, SeekOrigin.End);        

I have searched around but there are a couple of concepts here and I can't put them all together. 我已经四处寻找,但这里有几个概念,我不能把它们放在一起。

You can use the async { .. } workflow: 您可以使用async { .. }工作流程:

let createFileAsync (filePath, bytes) = async {
  use sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate)
  sourceStream.Seek(0, SeekOrigin.End)
  do! sourceStream.AsyncWrite(bytes, 0, bytes.Length) }

This is fairly imperative code, so it does not look very different in F#. 这是相当迫切的代码,因此它在F#中看起来并没有什么不同。

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

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