簡體   English   中英

如何在F#中編寫此異步IO C#代碼?

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

我有這個代碼:

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);
        }
    }

我想在F#上寫它,在我無法弄清楚該怎么做之前我得到了這個:

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

我已經四處尋找,但這里有幾個概念,我不能把它們放在一起。

您可以使用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) }

這是相當迫切的代碼,因此它在F#中看起來並沒有什么不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM