2012-05-05 47 views
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)在後臺運行,當我打電話讓用戶的列表(我已經調試後執行。 如何在動作完成前運行它? 感謝您的提前!

回答

1

這裏是它如何工作的:

BeginGetResponse -> makes an asynchronous web request 
asynchronous request completed -> makes a callback: ReadWebRequestCallback 
ReadWebRequestCallback -> read the response 

如此看來,要等待ReadWebRequestCallbackGETUserList()到達return語句之前......那麼不發出異步請求。做其他事情有效地破壞了發佈異步請求的目的。

您可以使用手動重置事件:

public class GETUser 
{ 
    public ManualResetEvent mrse = new ManualResetEvent(false); 
    // other fields 
} 

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(); 

    user.mrse.WaitOne(); // <--- block until the event is raised 
    return user.result; 
} 

而且回調:

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(); 

    mrse.Signal();// <-- signal that you have finished reading 
} 

同樣,這違背了使異步Web的目的請求。

+0

對不起,我的英語不好。我編輯了 我想調用sendGETRequest獲取用戶列表我想將sendGETRequest的結果賦值給變量。 – chanhle

+0

mrse.Sigal()是否可用於windowphone7.1? – chanhle

+0

@chanhle,應該是......我會假設windowphone 7.1正在使用.NET框架,並且'ManualResetEvent'已經出現了一段時間:http://msdn.microsoft.com/en-us/library/system .threading.manualresetevent.aspx – Kiril