473,320 Members | 1,870 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,320 software developers and data experts.

Doubts in ThreadPool

Hi,

I am designing an window based application which needs to check it up nearly
2000 url's whether it is valid url or not using WebRequest method.

In this scenario i am using ThreadPool. While using ThreadPool it creates a
thread for each URL.

I need to manage 2000 url's with in 12 threads using ThreadPool.

How do i solve this ?

Thanks in advance

Mahendran
Nov 16 '05 #1
5 1305
This is not very professional ... and I haven't tested it ...

public delegate void UrlReply(string reply);

private int workingThreads=0;
private urlReply=new UrlReply(this.UrlReply);

private System.Threading.WaitCallback callUrl=new
System.Threading.WaitCallback(this.CallUrl);

public void CheckUrls()

{

foreach (System.Uri url in urlCollection)

{
while (workingThreads>11) Application.DoEvents();

System.Threading.ThreadPool.QueueUserWorkItem(call Url,(object)url);

workingThreads++;

}

}

protected void CallUrl(object state)

{

string reply="Url is ok!";

System.Uri url=(System.Uri)state;

System.Net.WebRequest webRequest=System.Net.WebRequest.Create(url);

try{webRequest.GetResponse();}

catch(System.Exception e){reply=e.Message;}

this.Invoke(this.UrlReply,new object[]{reply});

}

protected void UrlReply(string reply)

{

workingThreads--;

}
"Mahendran" <ma*******@vishwak.com> schrieb im Newsbeitrag
news:uZ*************@TK2MSFTNGP12.phx.gbl...
Hi,

I am designing an window based application which needs to check it up nearly 2000 url's whether it is valid url or not using WebRequest method.

In this scenario i am using ThreadPool. While using ThreadPool it creates a thread for each URL.

I need to manage 2000 url's with in 12 threads using ThreadPool.

How do i solve this ?

Thanks in advance

Mahendran

Nov 16 '05 #2
Mahendran,

It does not create a thread for each url. Basically, you add work items
to the ThreadPool, and it processes those items. When one item completes,
it goes to the next in the list and processes that.

I would recommend processing the urls in batches in the thread pool, or
creating a handful of threads that will process batches of the urls.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mahendran" <ma*******@vishwak.com> wrote in message
news:uZ*************@TK2MSFTNGP12.phx.gbl...
Hi,

I am designing an window based application which needs to check it up
nearly
2000 url's whether it is valid url or not using WebRequest method.

In this scenario i am using ThreadPool. While using ThreadPool it creates
a
thread for each URL.

I need to manage 2000 url's with in 12 threads using ThreadPool.

How do i solve this ?

Thanks in advance

Mahendran

Nov 16 '05 #3
Hi Zürcher:

This routine isn't thread safe. The workingThreads field is shared
among the threads and great care must be taken when you increment,
decrement, and compare the value. See the Interlocked class in
System.Threading as a start.

HTH,

--
Scott
http://www.OdeToCode.com

On Thu, 19 Aug 2004 10:58:57 +0200, "Zürcher See"
<aq****@cannabismail.com> wrote:
This is not very professional ... and I haven't tested it ...

public delegate void UrlReply(string reply);

private int workingThreads=0;
private urlReply=new UrlReply(this.UrlReply);

private System.Threading.WaitCallback callUrl=new
System.Threading.WaitCallback(this.CallUrl);

public void CheckUrls()

{

foreach (System.Uri url in urlCollection)

{
while (workingThreads>11) Application.DoEvents();

System.Threading.ThreadPool.QueueUserWorkItem(call Url,(object)url);

workingThreads++;

}

}

protected void CallUrl(object state)

{

string reply="Url is ok!";

System.Uri url=(System.Uri)state;

System.Net.WebRequest webRequest=System.Net.WebRequest.Create(url);

try{webRequest.GetResponse();}

catch(System.Exception e){reply=e.Message;}

this.Invoke(this.UrlReply,new object[]{reply});

}

protected void UrlReply(string reply)

{

workingThreads--;

}
"Mahendran" <ma*******@vishwak.com> schrieb im Newsbeitrag
news:uZ*************@TK2MSFTNGP12.phx.gbl...
Hi,

I am designing an window based application which needs to check it up

nearly
2000 url's whether it is valid url or not using WebRequest method.

In this scenario i am using ThreadPool. While using ThreadPool it creates

a
thread for each URL.

I need to manage 2000 url's with in 12 threads using ThreadPool.

How do i solve this ?

Thanks in advance

Mahendran


Nov 16 '05 #4
Sorry, but I use an invoke to decrement it .. so is always the form thread
that increment and decrement it, or I miss something?

"Scott Allen" <bitmask@[nospam].fred.net> schrieb im Newsbeitrag
news:fa********************************@4ax.com...
Hi Zürcher:

This routine isn't thread safe. The workingThreads field is shared
among the threads and great care must be taken when you increment,
decrement, and compare the value. See the Interlocked class in
System.Threading as a start.

HTH,

--
Scott
http://www.OdeToCode.com

On Thu, 19 Aug 2004 10:58:57 +0200, "Zürcher See"
<aq****@cannabismail.com> wrote:
This is not very professional ... and I haven't tested it ...

public delegate void UrlReply(string reply);

private int workingThreads=0;
private urlReply=new UrlReply(this.UrlReply);

private System.Threading.WaitCallback callUrl=new
System.Threading.WaitCallback(this.CallUrl);

public void CheckUrls()

{

foreach (System.Uri url in urlCollection)

{
while (workingThreads>11) Application.DoEvents();

System.Threading.ThreadPool.QueueUserWorkItem(call Url,(object)url);
workingThreads++;

}

}

protected void CallUrl(object state)

{

string reply="Url is ok!";

System.Uri url=(System.Uri)state;

System.Net.WebRequest webRequest=System.Net.WebRequest.Create(url);

try{webRequest.GetResponse();}

catch(System.Exception e){reply=e.Message;}

this.Invoke(this.UrlReply,new object[]{reply});

}

protected void UrlReply(string reply)

{

workingThreads--;

}
"Mahendran" <ma*******@vishwak.com> schrieb im Newsbeitrag
news:uZ*************@TK2MSFTNGP12.phx.gbl...
Hi,

I am designing an window based application which needs to check it up

nearly
2000 url's whether it is valid url or not using WebRequest method.

In this scenario i am using ThreadPool. While using ThreadPool it
createsa
thread for each URL.

I need to manage 2000 url's with in 12 threads using ThreadPool.

How do i solve this ?

Thanks in advance

Mahendran

Nov 16 '05 #5
Hi Zürcher,

Apologies, I missed the Invoke call. It does look as if all the
manipulation happens on the same thread.

--
Scott
http://www.OdeToCode.com

On Thu, 19 Aug 2004 16:26:34 +0200, "Zürcher See"
<aq****@cannabismail.com> wrote:
Sorry, but I use an invoke to decrement it .. so is always the form thread
that increment and decrement it, or I miss something?


Nov 16 '05 #6

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

Similar topics

5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
5
by: Duane Pressley | last post by:
I'm looking for someone to help me make sense of the results I'm observing when using the ThreadPool class in a COM-Interop scenario. Here's the set up: 1.. A classic ASP page instantiates and calls...
1
by: doudou-shen | last post by:
I will use threadpool do some work with threadpool . but I haven't any information about it . who can help me! thank a lot
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.