Mohammed Noureldin
Mohammed Noureldin

Reputation: 16806

Does it make sense to call only one `async` method and `await` for it in another `async` method?

Does it make sense to call only one async private method and await for it in another async method? Example:

private async Task<string> ReadMyFileAsync()
{
    var streamReader = new StreamReader(@"c:\myFile.txt")

    return await streamReader.ReadToEndAsync(); // <-- Does it make sense for me here to await for only onething, while the caller of the method ReadMyFileAsync is already an async method?
    // By ignoring that the stream should be disposed...
}

private string ReadMyFile()
{
    var streamReader = new StreamReader(@"c:\myFile.txt")

    return streamReader.ReadToEnd();
    // By ignoring that the stream should be disposed...
}

public async void OnFileChangedHandler()
{
    // Some work...
    var myText = await ReadMyFileAsync(); <-- Does it make any difference whether I  called `await ReadMyFileAsync()` or `ReadMyFile()`?

    var someThingElse = await SomeThingElse(...);
    // Some work...
}

In the scenario above, what I guess, even if the method ReadMyFileAsync was called in its syncronous version (without using async and by using ReadToEnd() instead of await ReadToEndAsync()), it will keep acting the same, because the caller method is an async method. Which means in my opinion, this will make sense only if I am trying to read multiple files in paralell (see the following as example), is that right?

private async Task<string> ReadMyFileAsync() // <-- This makes sense for me
{
    var streamReader1 = new StreamReader(@"c:\myFile1.txt");
    var streamReader2 = new StreamReader(@"c:\myFile2.txt");
    var streamReader3 = new StreamReader(@"c:\myFile3.txt");

    var myText1Task = streamReader1.ReadToEndAsync();
    var myText2Task = streamReader2.ReadToEndAsync();
    var myText3Task = streamReader3.ReadToEndAsync();

    return await myText1Task + await myText2Task + await myText3Task;

    // By ignoring that the streams should be disposed...
}

So the question is simply, in the first block of code, does it make any difference whether I called ReadMyFileAsync or ReadMyFile?

Update: The whole question simplified is:

If I am going to await for the async method ReadMyFileAsync inside my async method OnFileChangedHandler, why would I use ReadMyFileAsync over ReadMyFile?

Upvotes: 2

Views: 1402

Answers (2)

Maxim Kosov
Maxim Kosov

Reputation: 1980

Yes, you should prefer async methods over sync because async methods doesn't block the executing thread. In UI apps it means that UI won't freeze. In Server apps it means that your server will be able to run more concurrent work without starving the threads.

Upvotes: -1

nvoigt
nvoigt

Reputation: 77285

It depends. If you really only call one other method and you have no side effects, then you could just return the Task instead of awaiting it and returning the result wrapped in a task.

However, even in your example, you have side effects. You need to .Dispose something and you need to know when to do that. So you cannot just return the task, you need to wait until it's finished, then dispose your resources and then return the result of the finished task. Since it's "not that simple" even in your own boiled down example, the answer is: it does make sense. You have to decide case-by-case.

Sometimes you can just pass the task along, then the method does not need to be async at all, it just needs to return a Task and sometimes you need to do things based on the timing of tasks (i.e. do this only after that other thing is finished) and then you need to use async methods because you want to await results.

Upvotes: 3

Related Questions