The use of threads in a ASP.NET app.

G

Guest

I am trying to write a basic load balancer (in our web service) solution. The
purpose of this load balancer is to keep an array updated with server status.
We have several servers that can be accessed in order to retrieve information.

From within the “Application_Start†(Global.asax), I create as many threads
as servers we have available. The idea is that each thread will query each
server and update a flag in the public array. When a “true†request comes in,
the system checks for two things:

* Server availability (on-line vs. off-line)
* Server load (based on a value returned by the server every time a thread
sends a new query)

I noticed that the threads may stop working. Sometimes they run for a long
time and sometimes they run for a short period of time. I started reading
some posts related to threading and I found this one “Threading in asp.net
issue - Thread stops†created on 11/12/2004 where “John Saunders†(who is
responding) says that the use of threads in a ASP.NET application is a bad
idea. He said “don't use threads with ASP.NET.â€

If this is true, how can I approach this load balancer project? Any ideas
will be greatly appreciated.

Thanks in advance.
 
B

bruce barker

there is nothing wrong with using threads in asp.net. asp.net will actualy
know nothing of threads you create, they are just standard .net threads.
they will run under the security context of the asp.net worker process. if
they are exiting earky, they are probably erroring out. if you have no error
logging featue in your thread, you need to add it.

note: to allow asp.net to unload its appdomains on recycle events, you
should check for the thread abort request.

-- bruce (sqlwork.com)


| I am trying to write a basic load balancer (in our web service) solution.
The
| purpose of this load balancer is to keep an array updated with server
status.
| We have several servers that can be accessed in order to retrieve
information.
|
| From within the "Application_Start" (Global.asax), I create as many
threads
| as servers we have available. The idea is that each thread will query each
| server and update a flag in the public array. When a "true" request comes
in,
| the system checks for two things:
|
| * Server availability (on-line vs. off-line)
| * Server load (based on a value returned by the server every time a thread
| sends a new query)
|
| I noticed that the threads may stop working. Sometimes they run for a long
| time and sometimes they run for a short period of time. I started reading
| some posts related to threading and I found this one "Threading in asp.net
| issue - Thread stops" created on 11/12/2004 where "John Saunders" (who is
| responding) says that the use of threads in a ASP.NET application is a bad
| idea. He said "don't use threads with ASP.NET."
|
| If this is true, how can I approach this load balancer project? Any ideas
| will be greatly appreciated.
|
| Thanks in advance.
|
 
K

Kevin Spencer

I noticed that the threads may stop working. Sometimes they run for a long
time and sometimes they run for a short period of time.

Would these threads stopping by any chance coincide with your app stopping?
The app will stop after a period of time with no requests. That would stop
any child threads.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
G

Guest

Thank you Bruce. Unswering your question, no, I don’t have any logging yet. I
was planning on doing this. You just mentioned something I wanted to ask. You
say that the threads I create are standard .NET threads and that they run
under the security context of the aspnet_wp process. I would like to be able
to monitor the threads I create. Is it enough to just check IsAlive property?

Could you please explain a bit more what you mean by unloading its
appdomains?
 
G

Guest

Thank you Kevin. I was not stopping/closing the app. You say that the app.
will stop after a period of time with no requests (this may be the case,
since I let sit there refreshing itself). Can this be changed or prevented?
 
K

Kevin Spencer

Can this be changed or prevented?

Yes, it can. No, it SHOULD not. Instead, you should build your app in such a
way that it recovers when the app restarts.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
G

Guest

Kevin, since the threads are being created in "Application_Start"
(Global.asax), shouldn't this take care of the re-creation of the threads
when the app. re-starts?
 
K

Kevin Spencer

Yes. If they stop working, they should start back up again when the app
restarts.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
A

Alvin Bruney [Microsoft MVP]

the use of threads in a ASP.NET application is a bad
idea. He said "don't use threads with ASP.NET."
It is not.

Though I cannot speak for John, I believe he may have been frustrated with
the large number of bad thread tutorials on the web and incorrect usage of
threading with regards to .NET.

Sometimes, I too, question the idea behind making threads so widely
accessible. Threading is inherently difficult to get right the first,
second, third or forth time around and requires deep understanding of
complicated stuff. .NET exposes the threadpool, to help manage some of these
issues and lighten the burden to the developer.

Thread on - even if it kills the server :)
--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
 
G

Guest

Thanks Alvin. I started looking into "threadpool" but when I read that there
was a 25 thread limitation I realized it was not going to be enough for what
I am trying to do.

Alvin Bruney said:
the use of threads in a ASP.NET application is a bad
idea. He said "don't use threads with ASP.NET."
It is not.

Though I cannot speak for John, I believe he may have been frustrated with
the large number of bad thread tutorials on the web and incorrect usage of
threading with regards to .NET.

Sometimes, I too, question the idea behind making threads so widely
accessible. Threading is inherently difficult to get right the first,
second, third or forth time around and requires deep understanding of
complicated stuff. .NET exposes the threadpool, to help manage some of these
issues and lighten the burden to the developer.

Thread on - even if it kills the server :)
--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
--------------------------------------------------


mareal said:
I am trying to write a basic load balancer (in our web service) solution.
The
purpose of this load balancer is to keep an array updated with server
status.
We have several servers that can be accessed in order to retrieve
information.

From within the "Application_Start" (Global.asax), I create as many
threads
as servers we have available. The idea is that each thread will query each
server and update a flag in the public array. When a "true" request comes
in,
the system checks for two things:

* Server availability (on-line vs. off-line)
* Server load (based on a value returned by the server every time a thread
sends a new query)

I noticed that the threads may stop working. Sometimes they run for a long
time and sometimes they run for a short period of time. I started reading
some posts related to threading and I found this one "Threading in asp.net
issue - Thread stops" created on 11/12/2004 where "John Saunders" (who is
responding) says that the use of threads in a ASP.NET application is a bad
idea. He said "don't use threads with ASP.NET."

If this is true, how can I approach this load balancer project? Any ideas
will be greatly appreciated.

Thanks in advance.
 
A

Alvin Bruney [Microsoft MVP]

when I read that there
was a 25 thread limitation I realized it was not going to be enough for
what

Now that's problem a design flaw. I've written a similar app a couple weeks
ago actually and the approach i took was to spin 3 threads and give them a
stack of urls to go hunt down. Each stack consisted of about 40,000 url's.
But the point is the cap on the thread use made the application behave
properly because there were at most 3 threads running on top. A couple more
underneath because of the way the webrequest mechanism works. but you get
the idea.

my rule of thumb for threading is to spin no more than 5 threads at any
point in time. even in low concurrency situations, such liberty can infact
cripple a beefy server.

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
--------------------------------------------------


mareal said:
Thanks Alvin. I started looking into "threadpool" but when I read that
there
was a 25 thread limitation I realized it was not going to be enough for
what
I am trying to do.

Alvin Bruney said:
the use of threads in a ASP.NET application is a bad
idea. He said "don't use threads with ASP.NET."
It is not.

Though I cannot speak for John, I believe he may have been frustrated
with
the large number of bad thread tutorials on the web and incorrect usage
of
threading with regards to .NET.

Sometimes, I too, question the idea behind making threads so widely
accessible. Threading is inherently difficult to get right the first,
second, third or forth time around and requires deep understanding of
complicated stuff. .NET exposes the threadpool, to help manage some of
these
issues and lighten the burden to the developer.

Thread on - even if it kills the server :)
--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
--------------------------------------------------


mareal said:
I am trying to write a basic load balancer (in our web service)
solution.
The
purpose of this load balancer is to keep an array updated with server
status.
We have several servers that can be accessed in order to retrieve
information.

From within the "Application_Start" (Global.asax), I create as many
threads
as servers we have available. The idea is that each thread will query
each
server and update a flag in the public array. When a "true" request
comes
in,
the system checks for two things:

* Server availability (on-line vs. off-line)
* Server load (based on a value returned by the server every time a
thread
sends a new query)

I noticed that the threads may stop working. Sometimes they run for a
long
time and sometimes they run for a short period of time. I started
reading
some posts related to threading and I found this one "Threading in
asp.net
issue - Thread stops" created on 11/12/2004 where "John Saunders" (who
is
responding) says that the use of threads in a ASP.NET application is a
bad
idea. He said "don't use threads with ASP.NET."

If this is true, how can I approach this load balancer project? Any
ideas
will be greatly appreciated.

Thanks in advance.
 
G

Guest

Alvin, so how would you tackle what I am trying to do. I may have 15 servers
to keep track of their status (working or not working). If you would not have
more than 5 threads are you saying that instead of creating 15 threads (in my
example) I should create 5 and have those 5 check the 15 servers?

Alvin Bruney said:
when I read that there
was a 25 thread limitation I realized it was not going to be enough for
what

Now that's problem a design flaw. I've written a similar app a couple weeks
ago actually and the approach i took was to spin 3 threads and give them a
stack of urls to go hunt down. Each stack consisted of about 40,000 url's.
But the point is the cap on the thread use made the application behave
properly because there were at most 3 threads running on top. A couple more
underneath because of the way the webrequest mechanism works. but you get
the idea.

my rule of thumb for threading is to spin no more than 5 threads at any
point in time. even in low concurrency situations, such liberty can infact
cripple a beefy server.

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
--------------------------------------------------


mareal said:
Thanks Alvin. I started looking into "threadpool" but when I read that
there
was a 25 thread limitation I realized it was not going to be enough for
what
I am trying to do.

Alvin Bruney said:
the use of threads in a ASP.NET application is a bad
idea. He said "don't use threads with ASP.NET."
It is not.

Though I cannot speak for John, I believe he may have been frustrated
with
the large number of bad thread tutorials on the web and incorrect usage
of
threading with regards to .NET.

Sometimes, I too, question the idea behind making threads so widely
accessible. Threading is inherently difficult to get right the first,
second, third or forth time around and requires deep understanding of
complicated stuff. .NET exposes the threadpool, to help manage some of
these
issues and lighten the burden to the developer.

Thread on - even if it kills the server :)
--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
--------------------------------------------------


I am trying to write a basic load balancer (in our web service)
solution.
The
purpose of this load balancer is to keep an array updated with server
status.
We have several servers that can be accessed in order to retrieve
information.

From within the "Application_Start" (Global.asax), I create as many
threads
as servers we have available. The idea is that each thread will query
each
server and update a flag in the public array. When a "true" request
comes
in,
the system checks for two things:

* Server availability (on-line vs. off-line)
* Server load (based on a value returned by the server every time a
thread
sends a new query)

I noticed that the threads may stop working. Sometimes they run for a
long
time and sometimes they run for a short period of time. I started
reading
some posts related to threading and I found this one "Threading in
asp.net
issue - Thread stops" created on 11/12/2004 where "John Saunders" (who
is
responding) says that the use of threads in a ASP.NET application is a
bad
idea. He said "don't use threads with ASP.NET."

If this is true, how can I approach this load balancer project? Any
ideas
will be greatly appreciated.

Thanks in advance.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top