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");
}
}
"John Saunders" wrote:
"Andrew Lippitt" <Andrew Li*****@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com... 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.
--
John Saunders
johnwsaundersiii at hotmail