locked
Return a Value from a Task RRS feed

  • Question

  • hello,

    i'm trying to retreive data from a Task via a web service but it generate an error while that

    it execute the asynchro function but can't retreive data with Task.result

    How can i fix it

    thanks for advance

    Saturday, March 29, 2014 10:21 AM

Answers

  • await will unwrap the task and reads it return. if there is something went wrong the exception will be throw. so your code will look more like this:

    void async SomeMethod()
    {
    try
    {
        ServiceClient proxy = new ServiceClient();
        var results = await proxy.CallAsync(param1, param2);
        // Do something with result
    }
    catch
    {
    //exception handling
    }
    }
      


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Anne Jing Tuesday, April 8, 2014 1:46 AM
    Sunday, March 30, 2014 3:43 PM

All replies

  • This should work.  
                int x = await GetValueSlow();
            }
    
            public async Task<int> GetValueSlow()
            {
                Task.Delay(1000);
                return 12;
            }

    Saturday, March 29, 2014 10:36 AM
  • i used this but it genetrate an error while retreiving data

    void async SomeMethod()
    {
        ServiceClient proxy = new ServiceClient();
        Task<T> results = await proxy.CallAsync(param1, param2);
        T result = results.Result;// here it generates the error
        if (result.Success)
        {
           // Do something with result
        }
    }
    

    Saturday, March 29, 2014 11:06 AM
  • await will unwrap the task and reads it return. if there is something went wrong the exception will be throw. so your code will look more like this:

    void async SomeMethod()
    {
    try
    {
        ServiceClient proxy = new ServiceClient();
        var results = await proxy.CallAsync(param1, param2);
        // Do something with result
    }
    catch
    {
    //exception handling
    }
    }
      


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Anne Jing Tuesday, April 8, 2014 1:46 AM
    Sunday, March 30, 2014 3:43 PM