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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:49:26+00:00 2026-06-14T16:49:26+00:00

I really hope there’s someone experienced enough both with TPL & System.Net Classes and

  • 0

I really hope there’s someone experienced enough both with TPL & System.Net Classes and methods

What started as a simple thought of use TPL on current sequential set of actions led me to a halt in my project.

As I am still fresh With .NET, jumping straight to deep water using TPL …

I was trying to extract an Aspx page’s source/content(html) using WebClient

Having multiple requests per day (around 20-30 pages to go through) and extract specific values out of the source code… being only one of few daily tasks the server has on its list,

Led me to try implement it by using TPL, thus gain some speed.

Although I tried using Task.Factory.StartNew() trying to iterate on few WC instances ,
on first try execution of WC the application just does not get any result from the WebClient

This is my last try on it

    static void Main(string[] args)
    {
        EnumForEach<Act>(Execute);
        Task.WaitAll();
    }

    public static void EnumForEach<Mode>(Action<Mode> Exec)
    {
            foreach (Mode mode in Enum.GetValues(typeof(Mode)))
            {
                Mode Curr = mode;

                Task.Factory.StartNew(() => Exec(Curr) );
            }
    }

    string ResultsDirectory = Environment.CurrentDirectory,
        URL = "",
        TempSourceDocExcracted ="",
        ResultFile="";

        enum Act
        {
            dolar, ValidateTimeOut
        }

    void Execute(Act Exc)
    {
        switch (Exc)
        {
            case Act.dolar:
                URL = "http://www.AnyDomainHere.Com";
                ResultFile =ResultsDirectory + "\\TempHtm.htm";
                TempSourceDocExcracted = IeNgn.AgilityPacDocExtraction(URL).GetElementbyId("Dv_Main").InnerHtml;
                File.WriteAllText(ResultFile, TempSourceDocExcracted);
                break;
            case Act.ValidateTimeOut:
                URL = "http://www.AnotherDomainHere.Com";
                ResultFile += "\\TempHtm.htm";
                TempSourceDocExcracted = IeNgn.AgilityPacDocExtraction(URL).GetElementbyId("Dv_Main").InnerHtml;
                File.WriteAllText(ResultFile, TempSourceDocExcracted);
                break;
        }

        //usage of HtmlAgilityPack to extract Values of elements by their attributes/properties
        public HtmlAgilityPack.HtmlDocument AgilityPacDocExtraction(string URL)
        {
            using (WC = new WebClient())
            {
                WC.Proxy = null;
                WC.Encoding = Encoding.GetEncoding("UTF-8");
                tmpExtractedPageValue = WC.DownloadString(URL);
                retAglPacHtmDoc.LoadHtml(tmpExtractedPageValue);
                return retAglPacHtmDoc;
            }
        }

What am I doing wrong? Is it possible to use a WebClient using TPL at all or should I use another tool (not being able to use IIS 7 / .net4.5)?

  • 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-14T16:49:27+00:00Added an answer on June 14, 2026 at 4:49 pm

    I see at least several issues:

    1. naming – FlNm is not a name – VisualStudio is modern IDE with smart code completion, there’s no need to save keystrokes (you may start here, there are alternatives too, main thing is too keep it consistent: C# Coding Conventions.

    2. If you’re using multithreading, you need to care about resource sharing. For example FlNm is a static string and it is assigned inside each thread, so it’s value is not deterministic (also even if it was running sequentially, code would work faulty – you would adding file name in path in each iteration, so it would be like c:\TempHtm.htm\TempHtm.htm\TempHtm.htm)

    3. You’re writing to the same file from different threads (well, at least that was your intent I think) – usually that’s a recipe for disaster in multithreading. Question is, if you need at all write anything to disk, or it can be downloaded as string and parsed without touching disk – there’s a good example what does it mean to touch a disk.

    4. Overall I think you should parallelize only downloading, so do not involve HtmlAgilityPack in multithreading, as I think you don’t know it is thread safe. On the other hand, downloading will have good performance/thread count ratio, html parsing – not so much, may be if thread count will be equal to cores count, but not more. Even more – I would separate downloading and parsing, as it would be easier to test, understand and maintain.

    Update: I don’t understand your full intent, but this may help you started (it’s not production code, you should add retry/error catching, etc.).
    Also at the end is extended WebClient class allowing you to get more threads spinning, because by default webclient allows only two connections.

    class Program
    {
        static void Main(string[] args)
        {
            var urlList = new List<string>
                              {
                                  "http://google.com",
                                  "http://yahoo.com",
                                  "http://bing.com",
                                  "http://ask.com"
                              };
    
            var htmlDictionary = new ConcurrentDictionary<string, string>();
            Parallel.ForEach(urlList, new ParallelOptions { MaxDegreeOfParallelism = 20 }, url => Download(url, htmlDictionary));
            foreach (var pair in htmlDictionary)
            {
                Process(pair);
            }
        }
    
        private static void Process(KeyValuePair<string, string> pair)
        {
            // do the html processing
        }
    
        private static void Download(string url, ConcurrentDictionary<string, string> htmlDictionary)
        {
            using (var webClient = new SmartWebClient())
            {
                htmlDictionary.TryAdd(url, webClient.DownloadString(url));
            }
        }
    }
    
    public class SmartWebClient : WebClient
    {
        private readonly int maxConcurentConnectionCount;
    
        public SmartWebClient(int maxConcurentConnectionCount = 20)
        {
            this.maxConcurentConnectionCount = maxConcurentConnectionCount;
        }
    
        protected override WebRequest GetWebRequest(Uri address)
        {
            var httpWebRequest = (HttpWebRequest)base.GetWebRequest(address);
            if (httpWebRequest == null)
            {
                return null;
            }
    
            if (maxConcurentConnectionCount != 0)
            {
                httpWebRequest.ServicePoint.ConnectionLimit = maxConcurentConnectionCount;
            }
    
            return httpWebRequest;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems with jquery and I really hope someone out there can
I'm almost losing it, i really hope someone can help me out! I'm using
I have a very weird problem.. I really do hope someone has an answer
I have a really hard time understanding routes and I hope someone can help
Really hope someone can help me or at least point me in the right
I really hope some SQL guru out there can assist with this one (and
I really hope someone can help on this because I'm learning cocoa and have
I'm struggling to meet a demand from my supervisors. I really hope that someone
I hope there is someone who can help me out here. I have found
I think I'm stuck and I really hope someone can help me out here.

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.