ThreadPool

G

Guest

I've written a c# web service. When a request is received I need to download
a potentially large file from another site. To do that I was starting a new
thread using QueueUserWorkItem but to my surprise the thread frequently
vanishes before finishing it's task. Why?
If I use system.threading.thread to create a thread instead of threadpool,
will it suffer the same fate? Is there any downside to that approach. Other
alternatives before resorting to an out of process solution?
 
B

Brock Allen

What do you mean vanishes? Is this code running in ASP.NET or in your NT
service (I can't tell from your post). How long is the application running
if it's a NT Service?
 
G

Guest

Brock:
My asp.net webservice code calls QueueUserWorkitem. The thread created by
thread pool writes some information to a sql table indicating it is about to
begin downloading a file then begins a download. After completing the
download it writes another entry to the same table indicating download is
complete then sends an email. Many times the file is downloaded and the email
sent. other times the thread simply vanishes before completing all the
tasks. I see the 1st entry indicating start of the download and then nothing
or sometimes the download completes but no email is sent. I do not know at
this point what is killing the thread.
 
B

Brock Allen

I'd put a try/catch around all the code in your target method of QUWI and
log when you enter the catch block. The only thing I'm guessing is that either
you have a bug somewhere or the AppDomain or worker process is somehow getting
shutdown while you're in the middle of your work. Logging the catch should
give you more info.
 
G

Guest

I have that already - I never see a log entry from the catch block. That's
what I mean by vanish.
jim kane
 
B

Brock Allen

So do you think the appdomain is restarting? Or the worker process is shutting
down?
 
G

Guest

Using performance monitor on the Asp.net worker process I can see my thread
created and then vanish. I just have no idea why the worker process is
resetting. If I could stop that my problem would be gone.
This article and code says if I use system threading the thread does not get
created by the worker process but from what I see in performance monitor that
isnt true.
http://www.codeproject.com/aspnet/a...sp?df=100&forumid=180375&exp=0&select=1113886

Unless I'm missing something the only way I can be sure my thread will not
vanish is create it outside of asp.net but I was hoping there was a setting
to prevent asp.net from killing my thread. Seems like quite a design flaw.
 
B

Brock Allen

Well, threads running in the thread pool are background threads, so their
presence doesn't keep the process running. I'd say create your own thread
and see if that makes things better. Beware of creating a new thread for
every request into your web service though... creating a thousand threads
won't scale so well :)
 
K

Kevin Spencer

When you use the ThreadPool, you are consuming one of the Threads that are
reserved for ASP.Net HttpHandlers. There is a limit to the number of Threads
that can be drawn from the ASP.Net ThreadPool. I haven't seen any
documentation indicating that the Worker Process would terminate any of
these threads, but it is certainly going to slow down your app's response
time. You might want to consider using a custom Thread instead.

Of course, this isn't what you're asking about. How are you downloading the
file? Are you creating a WebClient, or using a WebRequest? If so, do you
have any Try/Catch in the method that does the actual downloading? A
download can time out, and an unhandled Timeout exception would cause the
Thread to abort.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top