How to use async with await common for windows and Android ?

Hello,

The following is my async with await function.

public async static Task GetHeader(string inputParameters, string output1)
{

     var res = Task.Factory.FromAsync<string>(_client.BeginGetHeader(inputParameters, output1, DeviceTokenEntry, null, null), ar    =>                     _client.EndGetHeader(ar));

    //For Android
            res.Wait();
    //For Windows
             await res;

  return res.Result;

}

In the above function we can find
//For Android
res.Wait();
//For Windows
await res;

But i want a common await line for Windows and Android.

Thanks,
Naresh K

Posts

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    You should have a common await line for both clients. Here you can find more documentation on the subject: http://docs.xamarin.com/guides/cross-platform/advanced/async_support_overview/

    If you need anymore help with this subject, feel free to comment!

  • KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭

    As Jon indicates you should be able to use the async/await operators in Xamarin so the code should be the same. The task.Wait may cause a deadlock issue.

  • Naresh.5185Naresh.5185 USMember

    @JonDouglas‌

    Can you please give any more suggestions. The above URL, i have checked but it was not working to me.

    Thanks,
    Naresh

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    @Naresh.5185‌ Can you provide a Gist or Code snippet of what's not working for you? For both Android and Windows, you can choose to either use Task.Wait() or await. If you want to use await, you'll have to make the method you're in async and return a task. You can fire and forget about it, but that's not the preferred way of doing async/await.

    I'll be happy to help you further once I can see what you're doing, and where you want to call this method.

Sign In or Register to comment.