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 7533495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:44:35+00:00 2026-05-30T05:44:35+00:00

Here is the function I have now that obviously doesn’t work. The reason it

  • 0

Here is the function I have now that obviously doesn’t work. The reason it doesn’t work is because WebClient is asynchronous and data is empty before it gets filled by WebClient and crashes on the XML reader. How can I call WebClient within this function and still allow it to return ServerResult as required with or without needing an external event handler?

static public ServerResult isBarcodeCorrectOnServer(string barcode)
{
            Dictionary<string, IPropertyListItem> dict = configDictionary();

            string urlString = (string.Format("http://www.myurl.com/app/getbarcodetype.php?realbarcode={0}&type={1}", barcode, dict["type"]));

            WebClient wc = new WebClient();
            string data = "";
            wc.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    //Process the result...
                    data = e.Result;
                }
            };
            wc.DownloadStringAsync(new Uri(urlString));

            StringReader stream = new StringReader(data);
            var reader = XmlReader.Create(stream);
            var document = XDocument.Load(reader);
            var username = document.Descendants("item");
            var theDict = username.Elements().ToDictionary(ev => ev.Name.LocalName, ev => ev.Value);

            if (theDict.ContainsKey("type") == true && theDict["type"].ToString() == dict["type"].ToString())
            {
                return ServerResult.kOnServer;
            }
            else if (theDict.ContainsKey("type") == true)
            {
                return ServerResult.kWrongType;
            }
            else
            {
                return ServerResult.kNotOnServer;
            }
        }
  • 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-05-30T05:44:36+00:00Added an answer on May 30, 2026 at 5:44 am

    You can’t without “hacks” and you shouldn’t – embrace asynchrony and pass in a delegate that you want to be executed once the download is completed:

    static public void isBarcodeCorrectOnServer(string barcode, Action<string> completed)
    {
        //..
        wc.DownloadStringCompleted += (sender, e) =>
        {
           if (e.Error == null)
           {
                //Process the result...
                data = e.Result;
                completed(data);
           }
        };
        //..
    }
    

    You can move all your processing code now into a separate method which you call with the download result.

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

Sidebar

Related Questions

We have system here that uses Java JNI to call a function in a
Here's the story... I have a jQuery function that does something, this function is
Here is the situation. I have some javascript that looks like this: function onSubmit()
I have to work with some code that isn't truly MVC (i.e., it doesn't
Is it possible to have default arguments in MATLAB? For instance, here: function wave(a,
I have the function prototype here: extern C void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); I need to
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
Just a little confused here... I have a function in postgres, and when I'm
In python, you can have a function return multiple values. Here's a contrived example:
Okay here's the program I have typed up(stdio.h is included also): /* function main

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.