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

  • Home
  • SEARCH
  • 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 6962495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:40:01+00:00 2026-05-27T15:40:01+00:00

I am using the WebClient class and my own thin wrapper around it to

  • 0

I am using the WebClient class and my own thin wrapper around it to request JSON over HTML — I am on the recieving end of a Comet-like architecture and so the client needs to keep a pending request open at all times, so that the server can push to the client at will.

I am trying to confirm whether the following code would be a correct and safe way to do this:

      subscriber = new Action(() =>
      {
          // This function wraps the WebClient 'DownloadStringAsync' method
          client.BeginDownload(this.Path, new AsyncCallback(x =>
          {
              var data = (x.AsyncState as Func<JsonResponse>).EndInvoke(x);
              if (data != null)
              {
                  OnReceive(data, new Uri(this.FullUri));
              }
              subscriber(); // Received data so re-submit request

          }), this.QueryArgs);

          while (client.IsBusy)// As long as busy, no need to re-submit a request
          {
          }
          subscriber(); 
      });

      subscriber();

From my own analysis it looks to me like the above would ensure that the client always re-submits a request either when a) it receives a response, or b) it times out. Would this be an acceptable way to achieve this or is there a much better practice I am missing out on? Using the while-loop in this way seems nonstandard to me, however I can’t think of any other way to create a wait-until sort of loop.

I also want to make sure I don’t leak any resources with this setup, since it may or may not gaurantee that EndInvoke() is called every time?

  • 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-27T15:40:01+00:00Added an answer on May 27, 2026 at 3:40 pm

    I would be concerned that the recursive calls will eventually cause a stack overflow.

    If you run this for a few seconds, you’ll get a stack overflow error…

    Action<int> action = null;
    action = new Action<int>((i) =>
    {
        Console.WriteLine("Hello {0}", i);
        action(i+1);
    });
    action(1);
    

    I got to 12,181. How far did yours go? 🙂

    From the look of your code, it seems that you don’t actually need to call the Async method of WebClient. In fact, calling the direct download method might be more efficient than the tight loop you have.

    Perhaps something like this?

    var uri = new Uri("http://www.google.com");
    var client = new WebClient();
    while(true)
    {
        try
        {
            var data = client.DownloadData(uri);
            Console.WriteLine("Downloaded {0:N0} bytes.", data.Length);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the WebClient class in .NET 2.0 to perform an HTTP POST
I have a class that implements the IDisposable interface. I am using a webclient
I am using web client class to HTML data from a web page. Now
using (var client = new WebClient()) { html = client.DownloadString(some_string); //do something html =
I'm using .net httpwebrequest &/or webclient class How can i Progamatically pass a url
Good day. I'm using the WebClient class in my C# application in order to
We are downloading a full web page using System.Net.WebClient class. But we only want
When using the WebClient class you can check for errors and empty results by
All - I'm using C#'s WebClient class to gain access to a resource on
I'm using the WebClient class to download files from a web site and have

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.