Както каза АМД най-чистия начин е с Task и async/await..
using System.Threading.Tasks;
public static Task<string> GetUserSubscribeMethodAsync(User user)
{
if (user == null)
throw new ArgumentNullException("user");
var tcs = new TaskCompletionSource<string>();
string req_params = "token=" + user.Token.AccessToken;
try
{
PostJsonRequest<UserSubscribeStatus>(ContractConstants.QelloService.BILLING_USER_STATUS, req_params, response =>
{
if ((response.Error != null) || (response.Data == null) ||
(response.Data.ActiveRecurring == null) ||
(response.Data.ActiveRecurring.Length <= 0))
{
tcs.SetException(new UnableToGetUserSubscribeMethodException());
}
else
{
string paymentMethod = response.Data.ActiveRecurring[0].Trim();
tcs.SetResult(paymentMethod);
}
});
}
catch (Exception e)
{
tcs.SetException(e);
}
return tcs.Task;
}
// C# 5
string paymentMethod = null;
try
{
paymentMethod = await GetUserSubscribeMethodAsync(user);
}
catch (Exception e)
{
// handle any errors here (ArgumentNullException, UnableToGetUserSubscribeMethodException, etc.)
}