Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:06:56+00:00 2026-06-09T07:06:56+00:00

I have a method as follows: public decimal GetExchangeRate(string fromCurrency, string toCurrency) { GoogleCurrencyService

  • 0

I have a method as follows:

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

      } 

and another class as follows

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

the variable ExchangeRate always come out as zero, so I believe the function call “GetRateForCurrency” returns before the async callback gets called. How do I make sure that does not happen as I need the variable ExchangeRate to be set before being returned. Thanks. Also, I have noticed that the callback never gets called as I have a breakpoint in it and the exception as well which does not get called. So I donot know where the problem is.Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:06:58+00:00Added an answer on June 9, 2026 at 7:06 am

    You can use an event wait handle to block the current thread and wait for the async call…

    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;
        }
    }
    

    EDIT: The same class using an async pattern

    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;
        }
    }
    

    Consuming the async class:

    // 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.
                });
        }
    }
    

    Maybe you’ll have to dispatch the UI changes to ui thread. I not have WP framework here to confirm that this is the case, but I think it is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a method as follows :- public void setCurrencyCode(List<String>
So I have a class with a method as follows: public class SomeClass {
I have method that as follows public int TranslateOOV(string word, Stream logStream) { StreamWriter
I have a controller with an action method as follows: public class InventoryController :
In a customer class loader, I have a method findClass as follows: public Class
I have an extension method as follows: public static class PageExtensions { public static
I have a __new__ method as follows: class MyClass(object): def __new__(cls, *args): new_args =
I have a method in a class as follows... class foo{ int bar::randomNum10to50(){ srand
I have a class with a static method modifying a static variable as follows,
I have a domain class named Parent as follows public class Parent { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.