我的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