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?
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.To do that you need to be using a mechanism which exposes an asynchronous API, such as
FileStream
: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:
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.