我该如何处理这个windows phone webclient异步问题?

时间:2012-08-05 11:42:24

标签: c# .net windows-phone-7 asynchronous webclient

我有一个方法如下:

      public decimal GetExchangeRate(string fromCurrency, string toCurrency)
      {
            GoogleCurrencyService googleCurrencyService = new GoogleCurrencyService();
            return googleCurrencyService.GetRateForCurrency(fromCurrency, toCurrency);

      } 

和另一个课程如下

public class GoogleCurrencyService
{
    public decimal GetRateForCurrency(string fromCurrency, string toCurrency)
    {

        try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(StringDownloadCompleted);
            client.DownloadStringAsync(new Uri(_requestUri + fromCurrency + "=?" + toCurrency));

        }
        catch (Exception)
        {
            ExchangeRate = 0;
        }

        return ExchangeRate;
    }

    private void StringDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        _response = e.Result;
        ExchangeRate = ParseResponseAndGetExchangeRate();
    }

}//class GoogleCurrencyService

变量ExchangeRate总是为0,所以我相信在调用异步回调之前函数调用“GetRateForCurrency”会返回。我如何确保不会发生这种情况,因为我需要在返回之前设置变量ExchangeRate。谢谢。此外,我注意到回调永远不会被调用,因为我有一个断点,并且异常也没有被调用。所以我不知道问题出在哪里。任何帮助都表示赞赏。

5 个答案:

答案 0 :(得分:1)

您可以使用事件等待句柄来阻止当前线程并等待异步调用...

public class GoogleCurrencyService
{
    private const string RequestUri = "http://www.google.com/ig/calculator?hl=en&q=1{0}%3D%3F{1}";

    public decimal ExchangeRate { get; private set; }

    public decimal GetRateForCurrency(string fromCurrency, string toCurrency)
    {
        ExchangeRate = 0;
        // use a signaler to block this thread and wait for the async call.
        var signaler = new ManualResetEvent(false);
        try
        {
            var client = new WebClient();
            client.DownloadStringCompleted += StringDownloadCompleted;
            // pass the signaler as user token
            client.DownloadStringAsync(new Uri(String.Format(RequestUri, fromCurrency, toCurrency)), signaler);

            // wait for signal, it will be set by StringDownloadCompleted
            signaler.WaitOne();
        }
        finally
        {
            signaler.Dispose();
        }

        return ExchangeRate;
    }

    private void StringDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            ExchangeRate = ParseResponseAndGetExchangeRate(e.Result);
        }
        finally
        {
            // set signal
            ((ManualResetEvent)e.UserState).Set();
        }
    }

    private decimal ParseResponseAndGetExchangeRate(string result)
    {
        return 123;
    }
}

编辑:使用异步模式的同一个类

public class GoogleCurrencyService
{
    private const string RequestUri = "http://www.google.com/ig/calculator?hl=en&q=1{0}%3D%3F{1}";

    public void GetRateForCurrency(string fromCurrency, string toCurrency, Action<decimal> callback)
    {
        var client = new WebClient();
        client.DownloadStringCompleted += StringDownloadCompleted;
        // pass the callback as user token
        client.DownloadStringAsync(new Uri(String.Format(RequestUri, fromCurrency, toCurrency)), callback);
    }

    private void StringDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        // parse response to get the rate value
        var rate = ParseResponseAndGetExchangeRate(e.Result);

        // if a callback was specified, call it passing the rate.
        var callback = (Action<decimal>)e.UserState;
        if (callback != null)
            callback(rate);
    }

    private decimal ParseResponseAndGetExchangeRate(string result)
    {
        return 123;
    }
}

使用异步类:

// this is your UI form/control/whatever
public class MyUI
{
    public void OnButtonToGetRateClick()
    {
        var from = "USD"; // or read from textbox...
        var to = "EUR";

        // call the rate service
        var service = new GoogleCurrencyService();
        service.GetRateForCurrency(from, to, (rate) =>
            {
                // do stuff here to update UI.
                // like update ui.
            });
    }
}

也许您必须将UI更改分发给ui线程。我这里没有WP框架来确认是这种情况,但我认为确实如此。

答案 1 :(得分:0)

当您运行异步方法时,您将获得

的已完成方法的结果

StringDownloadCompleted

所以在您的代码中,您调用异步方法并立即返回ExchangeRate,它始终为0。

您必须在已完成的方法StringDownloadCompleted

中获取ExchangeRate

如果你想让你的GetRateForCurrency中的ExchangeRate进行同步调用

client.DownloadString(new Uri(_requestUri + fromCurrency + "=?" + toCurrency));

答案 2 :(得分:0)

这是你需要做的。

你在课堂上创建一个活动。 在您的代码中,您将激活异步Web客户端调用。调用完成后,您将包装数据并设置事件。我倾向于定义可以保存数据的事件参数。

一旦设置了该事件,呼叫者就会收到通知。

如果你想要一个例子,请看一下我帖子中的来源 http://invokeit.wordpress.com/2012/06/30/bing-mapcontrol-offline-tiles-solution-wpdev-wp7dev/

它是bing maps样本的扩展,它包含地址查找器类。看看如何解雇它以及如何通知客户

答案 3 :(得分:0)

ok打电话给uri后等待

public decimal GetRateForCurrency(string fromCurrency,string toCurrency)     {

    try
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(StringDownloadCompleted);
        client.DownloadStringAsync(new Uri(_requestUri + fromCurrency + "=?" + toCurrency));
        Thread.sleep(500000); //waiting
    }
    catch (Exception)
    {
        ExchangeRate = 0;
    }

    return ExchangeRate;
}

所以设置一个类似标签的webcontrol并执行此操作

 private void StringDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    _response = e.Result;
    yourlabel.Text = _response ;
    ExchangeRate = ParseResponseAndGetExchangeRate();
}

答案 4 :(得分:0)

此处您唯一的选择是使GetRateForCurrency异步(这意味着提升自己的Completed事件)。

是否支持TPL,你可以使用Task<T>作为将异步包装在链中的好方法,但不幸的是WP7不支持它。

作为替代方案,以及我所做的,是使用Reactive Extensions(Microsoft.Phone.Reactive)并在链中传递IObservable - 但是,如果你是Rx则需要学习很多东西只会在这一场景中使用它。