Understanding async - can I await a synchronous me

2019-05-02 06:57发布

Trying to understand async-await in C#, and a bit stuck in a "chicken and egg" problem, maybe.

Does an async method need to call another async for it to be asynchronous?

As a high level example, I'm trying to do a simple write to the file system, but not sure how I can make this task awaitable, if at all.

public Task<FileActionStatus> SaveAsync(path, data)
{
    // Do some stuff, then...

    File.WriteAllBytes(path, data); // <-- Allow this to yield control?

    // ... then return result
}

That line of code is being called within a method that I'm trying to make asynchronous. So while the file is being written, I'd like to yield control to the application, but not quite sure how to do that.

Can someone enlighten me with a very high-level example of how I could write a file to the file system in an async way?

2条回答
趁早两清
2楼-- · 2019-05-02 07:28

Does an async method need to call another async for it to be asynchronous?

Usually that is the case as async goes all the way to the bottom of the call-stack and ends at the lowest place which is actually making the IO operation. In your case, you're using File.WriteAllBytes which is a blocking synchronous call. You can't magically make it asynchronous.

Can someone enlighten me with a very high-level example of how I could write a file to the file system in an async way?

To do that you need to be using a mechanism which exposes an asynchronous API, such as FileStream:

public async Task<FileActionStatus> SaveAsync(string path, byte[] data) 
{
    using (FileStream sourceStream = new FileStream(path,
    FileMode.Append, FileAccess.Write, FileShare.None,
    bufferSize: 4096, useAsync: true))
    {
        await sourceStream.WriteAsync(data, 0, data.Length);
    }
    // return some result.
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-02 07:37

In addition to Yuval answer: in case if your classes don't have any async methods, you run the method on a background thread by using Task.Run so your code could look like this:

public Task<FileActionStatus> SaveAsync(path, data)
{
  return Task.Run<FileActionStatus>(() =>
    {
      File.WriteAllBytes(path, data);
      // don't forget to return your FileActionStatus
    });
}

Update: threads take resources. Think twice how you are going to use them. Async wrapping of a sync operation could give you benefits at least in UI responsiveness and parallelism. Test different scenarios before choose one. In other cases, use single thread.

查看更多
登录 后发表回答