Do you have to put Task.Run in a method to make it async?

I’m trying to understand async await in the simplest form. I want to create a very simple method that adds two numbers for the sake of this example, granted, it’s no processing time at all, it’s just a matter of formulating an example here.

Example 1

private async Task DoWork1Async()
{
    int result = 1 + 2;
}

Example 2

private async Task DoWork2Async()
{
    Task.Run( () =>
    {
        int result = 1 + 2;
    });
}

If I await DoWork1Async() will the code run synchronously or asynchronously?

Do I need to wrap the sync code with Task.Run to make the method awaitable AND asynchronous so as not to block the UI thread?

I’m trying to figure out if my method is a Task or returns Task<T> do I need to wrap the code with Task.Run to make it asynchronous.

I see examples on the net where people are awaiting code that has nothing async within and not wrapped in a Task.Run or StartNew.

3 Answers
3

Leave a Comment