C# Async return value

Hi,

Here is some code I got from a previous solution. Is it possible to get a return value from an Async call without the caller being an async. I am probably not understanding it exactly - but it seems I have keep layering async calls on async calls to do it.

Anyway here is the code below.
Thanks,
Ward.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Async1
{
    class YourClass
    {
        async public Task<string> Test1()
        {
            string html = string.Empty;

            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                System.Net.Http.HttpResponseMessage response = await client.GetAsync("http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28686082.html");

                html = await response.Content.ReadAsStringAsync();
            }

            return html;
        }
    }

    // http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28686082.html

    class Program
    {
        static void Main(string[] args)
        {
            YourClass instance = new YourClass();

            Task t = instance.Test1().ContinueWith(AfterDownload);

           // So I would like to be able to get return value from the function without having to make this an Async method.
          // So not using the ContinueWith method above.
          // Is this possible?

            t.Wait();

            Task x = instance.Test1();

            Console.ReadKey();

        }

        private static void AfterDownload(Task<string> html)
        {
            Console.WriteLine(html.Result);
        }

    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Yes, you can, but you cannot use the await keyword. You simply call the async method without await and leave the async off of the caller. Your async method returns a Task, so just capture that and inspect its Result property. Keep in mind that inspecting Result is blocking, so you will no longer be async.

e.g.

Task t = instance.Test1()
string html = t.Result;

Open in new window


The unspoken idea behind async/await is that you do make things async all the way down--unless there's a reason why you need to go synchronous, and then you'd refer to the Result property as above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.