I have been searching for a quite time but I can't seem to find a solution for my code.
How can I handle the exception when it goes to the if (e.Error != null)
condition, by other words, when something wrong happens with the async call?
private static StorageClient client = new StorageClient();
private static void TransferCompletion<T>(TaskCompletionSource<T> tcs, System.ComponentModel.AsyncCompletedEventArgs e, Func<T> getResult)
{
if (e.Error != null)
{
MessageBox.Show("1");
tcs.TrySetException(e.Error);
}
else if (e.Cancelled)
{
MessageBox.Show("2");
tcs.TrySetCanceled();
}
else
{
MessageBox.Show("3");
tcs.TrySetResult(getResult());
}
}
public static Task<getAllProvidersCompletedEventArgs> GetAllProvidersTask(this StorageClient client)
{
var tcs = new TaskCompletionSource<getAllProvidersCompletedEventArgs>();
client.getAllProvidersCompleted += (s, e) => TransferCompletion(tcs, e, () => e);
client.getAllProvidersAsync();
return tcs.Task;
}
And this is how I call it in my main:
var client = new StorageClient();
var providers_data = await client.GetAllProvidersTask();
I followed this example to write my code: How can I use async/await to call a webservice?