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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:15+00:00 2026-06-13T19:07:15+00:00

I ahve been diggin the net for some time now, not finding code examples

  • 0

I ahve been diggin the net for some time now, not finding code examples that help me through my problem.. I have looked at example code but I’m still not “getting” it…

I have read up on,

http://msdn.microsoft.com/en-us/library/aa480507.aspx and

http://msdn.microsoft.com/en-us/library/dd781401.aspx

But I cant seem to get it to work..

Im using HTMLAGILITYPACK

Today I make up to 20 webrequests,

After a request has finished, result is added to dictionary, after that a method searches it for the information, if found the code exits if not it makes another webrequest , until it caps at 20. I need to be able to exit all threads async calls when everything is found.

It goes like this

public void FetchAndParseAllPages()
    {
        PageFetcher fetcher = new PageFetcher();
        for (int i = 0; i < _maxSearchDepth; i += _searchIncrement)
        {
            string keywordNsearch = _keyword + i;
            ParseHtmldocuments(fetcher.GetWebpage(keywordNsearch));
            //this checks if the information was found or not, if 
            //found stop exit and add to database

            if (GetPostion() != 201)
            {   //ADD DATA TO DATABASE
                InsertRankingData(DocParser.GetSearchResults(), _theSearchedKeyword);
                return;
            }
        }
    }

This is inside the class that fetches the page

    public HtmlDocument GetWebpage(string urlToParse)
    {

        System.Net.ServicePointManager.Expect100Continue = false;
        HtmlWeb htmlweb = new HtmlWeb();
        htmlweb.PreRequest = new   HtmlAgilityPack.HtmlWeb.PreRequestHandler(OnPreRequest);
        HtmlDocument htmldoc = htmlweb.Load(@"urlToParse", "38.69.197.71", 45623, "PORXYUSER", "PROXYPASSWORD");

        return htmldoc;       
    }

    public bool OnPreRequest(HttpWebRequest request)
    {
       // request.UserAgent = RandomUseragent();
        request.KeepAlive = false;
        request.Timeout = 100000;
        request.ReadWriteTimeout = 1000000; 
        request.ProtocolVersion = HttpVersion.Version10;
        return true; // ok, go on
    }

How can I make this async and make it really quick with threads? Or should i even use threads when doing it async?

  • 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-13T19:07:16+00:00Added an answer on June 13, 2026 at 7:07 pm

    Okay I solved it! At least I think so! Execution time went down to around seven seconds. It took me about 30 secs to do that without async.

    Here my code for future reference. EDIT I used a console project to test the code. Also I’m using html agilitypack. This is my way of doing it, any tips on how to further optimize this would be cool to see.

        public delegate HtmlDocument FetchPageDelegate(string url);
    
        static void Main(string[] args)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 10;
            FetchPageDelegate del = new FetchPageDelegate(FetchPage);
            List<HtmlDocument> htmllist = new List<HtmlDocument>();
            List<IAsyncResult> results = new List<IAsyncResult>();
            List<WaitHandle> waitHandles = new List<WaitHandle>();
    
            DateTime start = DateTime.Now;
            for(int i = 0; i < 200; i += 10)
            {
                string url = @"URLSTOPARSE YOU CHANGE IT HERE READ FROM LIST OR ANYTHING";
                IAsyncResult result = del.BeginInvoke(url, null, null);
                results.Add(result);
                waitHandles.Add(result.AsyncWaitHandle);
            }
    
            WaitHandle.WaitAll(waitHandles.ToArray());
    
            foreach (IAsyncResult async in results)
            {   
                FetchPageDelegate delle = (async as AsyncResult).AsyncDelegate as FetchPageDelegate;
                htmllist.Add(delle.EndInvoke(async));
            }
            Console.ReadLine();
    
        }
    
        static HtmlDocument FetchPage(string url)
        {
            HtmlWeb htmlweb = new HtmlWeb();
            HtmlDocument htmldoc = htmlweb.Load(url);
            return htmldoc; 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xml code that can ahve two forms : Form 1 <?xml
I have some c# code running on sharepoint that i use to check inside
I ahve a jquery code that get the siblings of a table td text.
I ahve this code in asp that has a paragraph and a link in
Hey there, why does this code not work? $qry = mysql_query(SELECT performerid,pic0 FROM .$table.
I currently have the following code that retrieves data from the database and then
I have some .jspf files that are fragments which I include in a new
I am having another problem with my table and checkboxes. I ahve gotten my
I am sure this should be easy but I ahve been going crazy trying
I ahve the following piece of code. I get a correctly filled vector. But

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.