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?