IHttpModule Event Threading

G

Guest

What gaurantees are there in the way of which thread the events are called from.

I've seen:
Thread A Begin
Thread B Begin
Thread A End
Thread A End

That seems to indicate that Begin and End can't be assumed to be called from the same thread, but can I assume that the Begin will happen on the same thread that executes the request?

I'm trying to use these events to log which thread is executing a request.

Furthermore, I'm trying to do the same thing with a SOAP extension. Would anyone know the answer or should I ask in the other forums?

Thanks,
Andrew Lippitt
 
J

John Saunders

Andrew Lippitt said:
What gaurantees are there in the way of which thread the events are called from.

I've seen:
Thread A Begin
Thread B Begin
Thread A End
Thread A End

That seems to indicate that Begin and End can't be assumed to be called
from the same thread, but can I assume that the Begin will happen on the
same thread that executes the request?
I'm trying to use these events to log which thread is executing a request.

Furthermore, I'm trying to do the same thing with a SOAP extension. Would
anyone know the answer or should I ask in the other forums?

In general, you can make no assumption about the order in which threads
execute.

Now, in this case, you'll need to be more specific about what you mean by
"Begin" and "End". The IHttpModule interface doesn't define any events.
 
G

Guest

Thanks for the quick reply!

I'll try to elaborate. I'm implementing IHttpModule in an attempt to trap the BeginRequest and EndRequest events that the HttpApplication interface it provides exposes. My goal is to use these events to write a log indicating which pages were executed on which threads.

What I need to know is what I can rely upon in terms of which thread these events will be fired from.

It seems that the normal case is that EndRequest is fired from the same thread that BeginRequest is. However, the following sample will demonstrate this is not always the case. When this is installed, firing off several requests simultaneously will sometimes cause the BeginRequest and EndRequest for a single request to be fired from two different threads.

I'd expect for example that if two requests happen simultaneously that two different threads would service them and I'd get something like:

Thread 1: BeginRequest
Thread 2: BeginRequest
Thread 1: EndRequest
Thread 2: EndRequest

What happens in the case that seems odd is that I'll get more along the lines of

Thread 1: BeginRequest
Thread 2: BeginRequest
Thread 1: EndRequest
Thread 1: EndRequest

My question is what CAN I assume about the threads that call these events and how they relate to the thread that is actually going to service the request. And by servicing the request, I mean can I assume that the thread that fires BeginRequest will be the thread that executes the System.Web.UI.Page that was requested?

using System;
using System.Web;

public class HttpEvents : IHttpModule
{
public void Init(HttpApplication _context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}

private void context_BeginRequest(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Thread " + System.Threading.Thread.CurrentThread.GetHashCode() + ": BeginRequest");
}

private void context_EndRequest(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Thread " + System.Threading.Thread.CurrentThread.GetHashCode() + ": EndRequest");
}
}
 
S

Scott Allen

Hi Andrew:

I've never found a guarantee that a request will be on the same thread
for the entire processing pipeline.
 
N

Natty Gur

Hi,

After HttpApplication.InitInternal set all required events that should
be execute by application, HttpApplication.ResumeSteps actually perform
them. Now if you use any reflection tool you will see that ResumeSteps
guarantee that all execute steps occurred in the same thread
(this.OnThreadEnter(); and this.OnThreadLeave();).
(You can look at my blog for full request pipeline desc. :
http://weblogs.asp.net/ngur/archive/2004/07/05/173301.aspx)

Anyway correct me if I wrong but I think you record the thread that
deals with the context event and not the thread that perform request
pipeline.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

John Saunders

Andrew Lippitt said:
Thanks for the quick reply!

I'll try to elaborate. I'm implementing IHttpModule in an attempt to trap
the BeginRequest and EndRequest events that the HttpApplication interface it
provides exposes. My goal is to use these events to write a log indicating
which pages were executed on which threads.
What I need to know is what I can rely upon in terms of which thread these
events will be fired from.

You can't rely on anything at all. That's my point.
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top