0
我有2個功能Asyncallback功能如何等待它完成,並得到結果時調用
public void sendGETRequest()
{
System.Uri targetUri = new System.Uri(urlAdress);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.Method = "GET";
request.Headers["X_USERNAME"] = username;
request.Headers["X_PASSWORD"] = password;
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
result = httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
if (Completed != null)
Completed();
}
}
當我打電話
GET user;
public String GETUserList()
{
user = new GET();
user.urlAdress = url + "user";
user.username = username;
user.password = password;
user.sendGETRequest();
user.Completed += OutUser;
if (Completed != null)
Completed();
return user.result;
}
void OutUser()
{
Debug.WriteLine(user.result);
}
如果我叫1個ACTION像這樣得到的用戶列表
String json = GETUserList();
Debug.WriteLine("Hllllllllllll" + json + "HHHHHHHHHHHHHH" + p.resultWords);
它返回
HllllllllllllHHHHHHHHHHHHHH
[{"id":"3","username":"a","password":"a"},{"id":"4","username":"bbbb\n","password":"aaa"},{,{"id":"13","username":"abcdef","password":"abcdef"},{"id{"id":"10","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"11","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"12","word":"asdas","definition":"asdasd","cardset_id":"1"},{"id":"13","word":"asdas","definition":"asdasd","cardset_id":"1"},{"id":"14","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"15","word":"AAAAddddA","definition":"aaa","cardset_id":"2"},{"id":"16","word":"asdas","definition":"asdasd","
我覺得功能ReadWebRequestCallback(IAsyncResult的callbackResult)在後臺運行,當我打電話讓用戶的列表(我已經調試後執行。 如何在動作完成前運行它? 感謝您的提前!
對不起,我的英語不好。我編輯了 我想調用sendGETRequest獲取用戶列表我想將sendGETRequest的結果賦值給變量。 – chanhle
mrse.Sigal()是否可用於windowphone7.1? – chanhle
@chanhle,應該是......我會假設windowphone 7.1正在使用.NET框架,並且'ManualResetEvent'已經出現了一段時間:http://msdn.microsoft.com/en-us/library/system .threading.manualresetevent.aspx – Kiril