Doubts in ThreadPool

  • Thread starter Thread starter Mahendran
  • Start date Start date
M

Mahendran

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
 
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(callUrl,(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,

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.
 
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

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(callUrl,(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 said:
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
 
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 said:
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

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(callUrl,(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 said:
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
 
Hi Zürcher,

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