ThreadPool problem

G

Guest

Hi,
I have a .NET class which uses the .NET threadpool (using QueueUserWorkItem)
to create about 8 threads, which do some work while the parent thread waits
for them all to complete, which works fine when referenced from a normal EXE.

When I reference this DLL in a webservice however, the threads seem to be
created one by one in order, rather than at the same time - it is as if there
is only one available thread in the ThreadPool and the rest are being queued.
I can see this as I have code that writes the execution order to the event
log. In the version referenced from the EXE the order they return is in
keeping with what I would expect based on how long each takes i.e., the
fastest returns first, the slowest last. From the webservice, they appear in
the other they are created. I changed the first thread to sleep for 10
seconds, and it in the webservice it still finished first. The same code from
the EXE and it finished last, as you would expect.

Any ideas on why this is happening as the performance loss when I use this
in the webservice is massive. I read that ASP.Net (or IIS) also uses the
ThreadPool, but the system isn't even loaded. I called GetAvailableThreads()
which returned 50.

Thanks,
DAve
 
D

David Browne

Dave said:
Hi,
I have a .NET class which uses the .NET threadpool (using
QueueUserWorkItem)
to create about 8 threads, which do some work while the parent thread
waits
for them all to complete, which works fine when referenced from a normal
EXE.

When I reference this DLL in a webservice however, the threads seem to be
created one by one in order, rather than at the same time - it is as if
there
is only one available thread in the ThreadPool and the rest are being
queued.
I can see this as I have code that writes the execution order to the event
log. In the version referenced from the EXE the order they return is in
keeping with what I would expect based on how long each takes i.e., the
fastest returns first, the slowest last. From the webservice, they appear
in
the other they are created. I changed the first thread to sleep for 10
seconds, and it in the webservice it still finished first. The same code
from
the EXE and it finished last, as you would expect.

Any ideas on why this is happening as the performance loss when I use this
in the webservice is massive. I read that ASP.Net (or IIS) also uses the
ThreadPool, but the system isn't even loaded. I called
GetAvailableThreads()
which returned 50.

I'm not sure what's exactly happening to you, but as you mentioned ASP.NET
uses the built-in threadpool. The consequence of this is that you aren't
supposed to use it in ASP.NET applications. Rewrite your code to use a
custom thread pool or to spawn threads and you will fix a latent bug in your
application, and your problem will probably go away.

David
 
G

Guest

Hi David,
Thanks for your reply. Interestingly, the problem only occurs on IIS6. On my
local machine which runs IIS 5.1, the problem does not happen, only when I
deploy to our Windows Server 2003 IIS6 box. It seems really odd to me that I
can't create more than one thread from the ThreadPool.

Thanks,
Dave
 
D

David Browne

Dave said:
Hi David,
Thanks for your reply. Interestingly, the problem only occurs on IIS6. On
my
local machine which runs IIS 5.1, the problem does not happen, only when I
deploy to our Windows Server 2003 IIS6 box. It seems really odd to me that
I
can't create more than one thread from the ThreadPool.

You can't create any threads from the thread pool. The thread pool creates
its own threads. When using the thread pool, you just add work to the
thread pool, and sometime later an availiable thread performs the work. Are
you sure you're coding this correctly?

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top