473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Confusing Asynchronous Threadpool

Hi all,

I have the following code I created to do multiple websites data crawling
using Asynchronous Thread calling it works fine however I'm confused when it
comes to make the calling thread stops or sleep until all other threads
within the threadpool have all finished their jobs.

what happens at the moment is that every other thread "should" just
concatenate string data to public string variable when it finishes, but
however the code will just go and execute the rest of the calling method
without all thread being completed.

I would be grateful if you could help me out with a solution on how to force
the code to wait for all methods to finish, here is the code, thank you:
//The calling method is in a different class, different file, it calls
ScanSites() method below passing an array of multiple web addresses
namespace Crawler

{

class Bot

{

public static string res = "";
public Bot()

{

}

public void ScanSites(string str)

{

// for each URL in the collection...
for (x = 0; x < URLs.Length- 1; x++)

{

WebRequest request = HttpWebRequest.Create(URLs[x]);

// RequestState is a custom class to pass info

RequestState state = new RequestState(request, URLs[x]);

IAsyncResult result = request.BeginGetResponse(new
AsyncCallback(UpdateItem), state);

ThreadPool.RegisterWaitForSingleObject(result.Asyn cWaitHandle, new
WaitOrTimerCallback(ScanTimeoutCallback), state, (30 * 1000), true);
}

}

private void UpdateItem(IAsyncResult result)

{

// grab the custom state object

RequestState state = (RequestState)result.AsyncState;

WebRequest request = (WebRequest)state.Request;

// get the Response

HttpWebResponse response =

(HttpWebResponse)request.EndGetResponse(result);

StringBuilder sb = new StringBuilder(4096);

byte[] buf = new byte[2048];

int count;

while ((count = response.GetResponseStream().Read(buf, 0, buf.Length - 8)) >
0)

{

sb.Append(Encoding.UTF8.GetString(buf, 0, count));

}

StreamWriter sw = new StreamWriter("C:\\Rep\\" + state.URL);

sw.Write(sb);

sw.Close();

//here is the "res" variable that will hold the URL address concatenated |
and followed by another URL retrieved from another thread

res += state.URL+ "|";


}

private static void ScanTimeoutCallback(object state, bool timedOut)

{

if (timedOut)

{

RequestState reqState = (RequestState)state;

if (reqState != null)

reqState.Request.Abort();

}

}

}

class RequestState

{

public WebRequest Request; // holds the request

public string URL;

// public object Data; // store any data in this

public RequestState(WebRequest request, string url)

{

this.Request = request;

this.URL= docid;

// this.Data = data;

}

}


May 29 '06 #1
3 2279
Maya Sam wrote:
I would be grateful if you could help me out with a solution on how to force
the code to wait for all methods to finish, here is the code, thank you:


Wow, that's some seriously ill-formatted code. Worse, it's missing a
} or two, so it took a few minutes to get it to a readable state.

Anyhow, probably the easiest way to do what you want is to not use
an aysnch callback, but to 1) build two lists, of WebRequest-s and
IAsyncResult-s 2) use BeginGetResponse to start each thread, passing
null, null and then 3) use EndGetResponse to block until each has
returned.

public void ScanSites2(string str)
{
// Start each request
List<WebRequest> Requests = new List<WebRequest>(URL.Length);
List<IAsyncResult> Cookies = new List<IAsyncResult>(URLs.Length);
foreach (string URL in URLS)
{
WebRequest request = HttpWebRequest.Create(URL);
Requests.Add(request);
Cookies.Add(request.BeginGetResponse(null, null));
}

// Block until all return
for (int Index = 0; Index < URLS.Length; Index++)
UpdateItem2(Requests[Index].EndGetResponse(Cookies[Index]);
}

private void UpdateItem2(HttpWebResponse response)
{
// It would be much easier to use response.GetResponseStream().ReadToEnd()
StringBuilder sb = new StringBuilder(4096);

byte[] buf = new byte[2048];

int count;

while ((count = response.GetResponseStream().Read(buf, 0, buf.Length - 8)) > 0)
{
sb.Append(Encoding.UTF8.GetString(buf, 0, count));
}

StreamWriter sw = new StreamWriter("C:\\Rep\\" + state.URL);
sw.Write(sb);
sw.Close();

//here is the "res" variable that will hold the URL address concatenated |
//and followed by another URL retrieved from another thread
res += state.URL + "|";
}
--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Being printed - in stores by June
May 29 '06 #2
Hi Jon,

Apologies for the misforamtting, I didn't think of the fact that you might
copy the code to try it at your side.

I have tried your code and it worked in a very nice way, many thanks for
that, one last question (I promise!), is there a way to catch the thread
getresponse exceptions? for example I get alot of timeout error messages
from sites I cant reach, the code doesn't do anything when a timeout occurs
in the thread and it stays forever without breaking. any ideas how to get
around this issue?

Many thanks again for your help.

Maya.

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:44***************@midnightbeach.com...
Maya Sam wrote:
I would be grateful if you could help me out with a solution on how to
force
the code to wait for all methods to finish, here is the code, thank you:


Wow, that's some seriously ill-formatted code. Worse, it's missing a
} or two, so it took a few minutes to get it to a readable state.

Anyhow, probably the easiest way to do what you want is to not use
an aysnch callback, but to 1) build two lists, of WebRequest-s and
IAsyncResult-s 2) use BeginGetResponse to start each thread, passing
null, null and then 3) use EndGetResponse to block until each has
returned.

public void ScanSites2(string str)
{
// Start each request
List<WebRequest> Requests = new List<WebRequest>(URL.Length);
List<IAsyncResult> Cookies = new List<IAsyncResult>(URLs.Length);
foreach (string URL in URLS)
{
WebRequest request = HttpWebRequest.Create(URL);
Requests.Add(request);
Cookies.Add(request.BeginGetResponse(null, null));
}

// Block until all return
for (int Index = 0; Index < URLS.Length; Index++)
UpdateItem2(Requests[Index].EndGetResponse(Cookies[Index]);
}

private void UpdateItem2(HttpWebResponse response)
{
// It would be much easier to use
response.GetResponseStream().ReadToEnd()
StringBuilder sb = new StringBuilder(4096);

byte[] buf = new byte[2048];

int count;

while ((count = response.GetResponseStream().Read(buf, 0, buf.Length -
8)) > 0)
{
sb.Append(Encoding.UTF8.GetString(buf, 0, count));
}

StreamWriter sw = new StreamWriter("C:\\Rep\\" + state.URL);
sw.Write(sb);
sw.Close();

//here is the "res" variable that will hold the URL address
concatenated |
//and followed by another URL retrieved from another thread
res += state.URL + "|";
}
--

.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Being printed - in stores by June

May 29 '06 #3
Maya Sam wrote:
Apologies for the misforamtting, I didn't think of the fact that you might
copy the code to try it at your side.
Well, it was less a matter of trying it than trying to read it.
I have tried your code and it worked in a very nice way, many thanks for
that, one last question (I promise!), is there a way to catch the thread
getresponse exceptions? for example I get alot of timeout error messages
from sites I cant reach, the code doesn't do anything when a timeout occurs
in the thread and it stays forever without breaking. any ideas how to get
around this issue?


Setting the WebRequest Timeout property should help. (Off the top of
my head, I don't know whether you'll get a WebException from
EndGetResponse or GetResponseStream.)

An alternative is to create a delegate to a method that takes a URL
and returns its contents (or null). This method would do a synchronous
GetResponse, which might make the error handling a bit more
straightforward. See, for example,
<http://www.devsource.com/article2/0,1895,1966478,00.asp>.
--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Being printed - in stores by June
May 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
1
by: colinjack | last post by:
Hi All, I've been using the original (non-event based) asynchronous design pattern using delegates. It works fine but performance wise I know using the ThreadPool is supposed to be far better,...
1
by: MSDN | last post by:
Does anyone know how to do this with a readline statement or equivalent method? Thanks in advance. Chris
1
by: dba123 | last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method which does an insert but how can I do this Asyncronously so that it does not affect the load on our public production...
3
by: Morgan Cheng | last post by:
Asynchronous I/O is said to be using native completion port. However, i checked rotor2.0 code, it seems not in stream.cs. BeginRead is defined in below way. In my understanding, If BeginInvoke is...
3
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I need to write asynchronous HTTPModule which will sometimes execute long job. I've written it using AddOnBeginRequestAsync but it still executes synchronously - I am checking performance...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.