Understanding async - can I await a synchronous method?

扶醉桌前 提交于 2020-01-13 10:46:06

问题


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?


回答1:


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.
}



回答2:


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.



来源:https://stackoverflow.com/questions/33844143/understanding-async-can-i-await-a-synchronous-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!