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
public class RequestState
{
public string _urlHtml = "";
public bool isCompleted = false;
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);
var myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(uri);
var myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest1;
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
{
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2 = myRequestState.request;
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse = responseStream;
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;
}
}
private void ReadCallBack(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead(asyncResult);
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
_thMain = new Thread(MainPageThreadfunction)
{
Priority = ThreadPriority.Highest,
IsBackground = true
};
_thMain.Start();
Thread functions
private void MainPageThreadfunction()
{
var uri = "https://www.TESTSITE.com/search?page=";
for(int i=0; i<5; i++)
{
var data = "";
var link = uri+ i;
var oPagent = new HttPagent();
oPagent.GetAsyncHtml(link );
while (!oPagent.isCompleted)
{
Application.DoEvents();
}
data = oPagent._urlHtml;
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)
{
}
Please check the code and let me know where i'm wrong.
Thanks
Kapil