Click here to Skip to main content
15,881,172 members

C# Windows threads with IAsyncResult

Kapil Waghe asked:

Open original thread
Hello,

I'm developing an C# windows application to pull data from main url and then I get the inner url's from main url data and then call the thread to get each inner url data.

The same is working but in the process unless and until all inner url data is not fetched the main url data extraction is stuck.

Like main url has 50 inner url, it runs all 50 urls and then goto next of main url.
But I want to run both threads parallel.

Below is the code : http://msdn.microsoft.com/en-IN/library/system.net.httpwebrequest.begingetresponse(v=vs.95).aspx

C#
public class RequestState
{
        public string _urlHtml = "";
        public bool isCompleted = false;

  // This class stores the State of the request.
  const int BUFFER_SIZE = 1024;
  public StringBuilder requestData;
  public byte[] BufferRead;
  public HttpWebRequest request;
  public HttpWebResponse response;
  public Stream streamResponse;

  public RequestState()
  {
    BufferRead = new byte[BUFFER_SIZE];
    requestData = new StringBuilder("");
    request = null;
    streamResponse = null;
  }
}

public class HttPagent
{
public static ManualResetEvent allDone= new ManualResetEvent(false);
  const int BUFFER_SIZE = 1024;


        public void GetAsyncHtml(string url)
        {
            try
            {
                var uri = new Uri(url);

                // Create a HttpWebrequest object to the desired URL.
                var myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(uri);

                // Create an instance of the RequestState and assign the previous myHttpWebRequest1
                // object to it's request field.  
                var myRequestState = new RequestState();
                myRequestState.request = myHttpWebRequest1;

                // Start the asynchronous request.
                IAsyncResult result =
                  myHttpWebRequest1.BeginGetResponse(RespCallback, myRequestState);

            }
            catch (WebException e1)
            {
                _urlHtml = "MZon-GetDataERROR" + e1.Message;
                isCompleted = true;
            }
            catch (Exception e2)
            {
                _urlHtml = "MZon-GetDataERROR" + e2.Message;
                isCompleted = true;
            }
        }

        private void RespCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                // State of request is asynchronous.
                RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
                HttpWebRequest myHttpWebRequest2 = myRequestState.request;
                myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);

                // Read the response into a Stream object.
                Stream responseStream = myRequestState.response.GetResponseStream();
                myRequestState.streamResponse = responseStream;

                // Begin the Reading of the contents of the HTML page and print it to the console.
                if (responseStream != null)
                {
                    IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0,
                                                                                  BUFFER_SIZE,
                                                                                  new AsyncCallback(ReadCallBack),
                                                                                  myRequestState);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
                isCompleted = true;
            }
        }

        //Read html callback
        private void ReadCallBack(IAsyncResult asyncResult)
        {
            try
            {
                RequestState myRequestState = (RequestState)asyncResult.AsyncState;
                Stream responseStream = myRequestState.streamResponse;
                int read = responseStream.EndRead(asyncResult);

                // Read the HTML page and then do something with it
                if (read > 0)
                {
                    myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
                    IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE,
                                                                               new AsyncCallback(ReadCallBack),
                                                                               myRequestState);
                }
                else
                {
                    if (myRequestState.requestData.Length > 1)
                    {
                        string stringContent;
                        stringContent = myRequestState.requestData.ToString();
                        _urlHtml = stringContent;
                    }

                    responseStream.Close();
                    allDone.Set();
                    isCompleted = true;
                }

            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
            }
        }
}



Button click

C#
_thMain = new Thread(MainPageThreadfunction)
                        {
                            Priority = ThreadPriority.Highest,
                            IsBackground = true
                        };
                    _thMain.Start();



Thread functions

C#
        //Main page thread function
        private void MainPageThreadfunction()
        {
var uri = "https://www.TESTSITE.com/search?page=";


for(int i=0; i<5; i++)
{

 //Get page data
                var data = "";
var link = uri+ i;
                var oPagent = new HttPagent();
                oPagent.GetAsyncHtml(link );

                while (!oPagent.isCompleted)
                {
                    Application.DoEvents();
                }

                data = oPagent._urlHtml;

// GET INNER LINKS CODE HERE... I USED REGULAR EXPRESSION

 MatchCollection mc = Regex.Matches(data, "REGULAREXPRESSION",
                                                   RegexOptions.IgnoreCase);

                foreach (Match match in mc)
                {
var URL= match.Groups["URL"].Value;
 var _thInner = new Thread(() => InnerPageThreadfunction(URL);
                        _thInner.Start();
}



}
}

      
        private void InnerPageThreadfunction(string url)
        {

//Inner thread url extract code here
}



Please check the code and let me know where i'm wrong.

Thanks
Kapil
Tags: C#, Windows, Threads, Asynchronous

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900