我的RestSharp实现有以下问题。如何在继续之前让我的应用程序等待来自ExecuteAsync()
的响应?等待ExecuteAsync()结果
我尝试了不同的解决方案:
优先(该方法不等待ExecuteAsync响应):
public Task<Connection> Connect(string userId, string password)
{
var client = new RestClient(_baseUrl)
{
Authenticator = new SimpleAuthenticator("user", userId,
"password", password)
};
var tcs = new TaskCompletionSource<Connection>();
var request = new RestRequest(AppResources.Authenticating);
client.ExecuteAsync<Connection>(request, response =>
{
tcs.SetResult(new JsonDeserializer().
Deserialize<Connection>(response));
});
return tcs.Task;
}
所以我想这一点,但应用程序冻结:
public Task<Connection> Connect(string userId, string password)
{
EventWaitHandle executedCallBack = new AutoResetEvent(false);
var client = new RestClient(_baseUrl)
{
Authenticator = new SimpleAuthenticator("user", userId,
"password", password)
};
var tcs = new TaskCompletionSource<Connection>();
var request = new RestRequest(AppResources.Authenticating);
client.ExecuteAsync<Connection>(request, response =>
{
tcs.SetResult(new JsonDeserializer().
Deserialize<Connection>(response));
executedCallBack.Set();
});
executedCallBack.WaitOne();
return tcs.Task;
}
什么是RestClient? – Default 2013-03-15 16:06:51
它来自'RestSharp'库,一种'WebClient' – user2169047 2013-03-16 09:41:39
什么是连接?我无法在RestSharp中找到此课程 – adrian4aes 2015-12-09 14:34:02