简体   繁体   English

如何让我的函数等待异步函数完成?

[英]How do I make my function wait until an async function has completed?

I need some code to allow me to know when an async thread has completed. 我需要一些代码来让我知道异步线程何时完成。 This is for my Windows phone app I'm making. 这是我正在制作的Windows Phone应用程序。 The app is a bus tracker to show live departures and data will be taken from online web page. 该应用程序是一个巴士追踪器,可显示实时发车信息,并且数据将从在线网页上获取。

I have this line of code in my GetDataFeed function 我的GetDataFeed函数中有这行代码

 // start the asynchronous request
IAsyncResult aResult =   BusStopFeedRequest.BeginGetResponse(new AsyncCallback(HandleFeedData), myState);

This will fire off my HandleFeedData function that will save a websites source code into a text string variable. 这将触发我的HandleFeedData函数,该函数会将网站源代码保存到文本字符串变量中。

My problem is the textbox on my phone app wont populate the content of the string as there is no data in string just yet. 我的问题是电话应用程序上的文本框无法填充字符串的内容,因为字符串中还没有数据。 This is called by textbox1.Text = obj.GetText() which should be returning the private string in my instantiated class. 这由textbox1.Text = obj.GetText()调用,该方法应在我实例化的类中返回私有字符串。

I have looked into using the aResult.AsyncWaitHandle.WaitOne() and tried polling for the aResult.IsCompleted() methods but from what I read and found myself, these just dont work. 我已经研究过使用aResult.AsyncWaitHandle.WaitOne()并尝试轮询aResult.IsCompleted()方法,但是从我的阅读和发现中发现,这些方法均aResult.IsCompleted() The WaitOne throws an unexpected error and the polling of IsCompleted is in an never ending loop. WaitOne引发意外错误,并且IsCompleted的轮询处于永无止境的循环中。

Has any one got any insights into what I can do to allow my function to wait for the async request to complete so it updates my class variable so my textbox can then see the data 有谁对我可以做些什么来让我的功能等待异步请求完成有任何见解,以便它更新我的类变量,以便我的文本框可以查看数据

In the windows phone, HTTPWebRequest contain BeginGetResponse and EndGetResponse method. 在Windows Phone中,HTTPWebRequest包含BeginGetResponse和EndGetResponse方法。 You need to add similar code on the function HanldeFeedData function. 您需要在函数HanldeFeedData函数上添加类似的代码。

HttpWebResponse response=(result.AsyncState as HttpWebrequest).EndGetResponse(result) as HttpWebResponse;
 string returnValue=null;
  using (StreamReader sr=new StreamReader(response.GetResponseStrem())
  {
      returnValue=sr.ReadToEnd();
   } 

   Dispatcher.BeginInvoke(new Action(()=>textbox1.Text=returnValue));

Are you certain that the string is actually being downloaded? 您确定该字符串实际上正在下载吗? "BeginGetResponse" is a function on the WebRequest class, which is not available on windows phone. “ BeginGetResponse”是WebRequest类上的一个函数,在Windows Phone上不可用。 It's possible that the data is not being downloaded. 数据可能没有下载。 However, if it is being downloaded, I suggest you do something like this: 但是,如果正在下载它,建议您执行以下操作:

IAsyncResult aResult = BusStopFeedRequest.BeginGetResponse(new AsyncCallback(HandleFeedData));

and then: 接着:

private void HandleFeedData(IAsyncResult result)
{
    string feedData = (string)result.AsyncState;
    textbox1.Text = feedData;
    // code to save the feedData here
}

However, it would be helpful to know exactly when BusStopFeedRequest's "BeginGetResponse" is fired. 但是,确切地知道何时触发BusStopFeedRequest的“ BeginGetResponse”会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM