473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

thread httpContext

If I launch a thread within an asp.net application. Is their anyway to get
at the base threads HttpContext object.

If the thread errors I want to capture stuff like:
HttpContext.Current.Request.ServerVariables["REMOTE_HOST"]
Jun 6 '07 #1
6 1927
Hi Chuck,

Regarding on the thread HttpContext issue you mentioned, do you mean you
want to access the HttpContext in a thread you manually started through
Thread.Start?

Based on my understanding, the HttpContext object is only associated with
those ASP.NET worker threads(pickup from thread pool when a request
coming). For those custom thread you start through Thread.Start, they're
not associated with a HttpContext object automatically. Would you provide
some further info about how to will spawn those custom threads and what
you'll do in it? Anyway, since custom thread's execution lifecycle may
beyound an ASP.NET request's server-side processing lifecycle, it is not
recommended(also unsafe) to use Httpcontext objects(like Request, Response)
in custom thread's code. If what you want to pass some unchanged
data/parameters, you can consider direclty pass them as Thread's input
parameters(State data) at startup time.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 7 '07 #2
thanks, that's whats I figured.
I am writing a ExceptionHandler : IHttpModule
that hooks into
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUhe);

incase some error occurs in a thread.

If the event does get called I'd like to know some things like
name of the server, name of the application, class that spawned the thread.
Jun 7 '07 #3
"Chuck P" <Ch***@newsgroup.nospamwrote in message
news:28**********************************@microsof t.com...
thanks, that's whats I figured.
I am writing a ExceptionHandler : IHttpModule
that hooks into
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUhe);

incase some error occurs in a thread.

If the event does get called I'd like to know some things like
name of the server, name of the application, class that spawned the
thread.
I don't believe you can find out the class that spawned the thread, unless
you give the thread that information when you start it.

What would your ExceptionHandler module do? It can't affect the current
request, as there may not be a current request by the time the thread throws
the unhandled exception.
--
John Saunders [MVP]
Jun 7 '07 #4
Hi Chuck,

As you mentioned that you are registering the
AppDomain.UnhandledExceptionEvent, why did you use Httpmodule? Httpmodule
will be called at each request while AppDomain event onlyl need to be
registered once.

Also, the "UnhanddledExceptionEvent" is actually a notify event, that means
it will inform you of such an unhandled exception, but you can not do
anything to prevent the application from being ended due to the unhandled
exception.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 8 '07 #5
Hi Steven,
I have bunches of asp.net applications.
Wanted to write a drop in Unhandled Exception utility we could use in all of
our applications.

In the module I monitor:
the application.Error event for UHE on the main thread.
and the AppDomain.CurrentDomain.UnhandledException for other threads.

Here is my code for the module. If their is a better way please let me know.

public virtual void Init(HttpApplication application)
{

application.Error += new EventHandler(OnError);

if (!_initialized)
{
lock (_initLock)
{
if (!_initialized)
{
if (application == null)
throw new ArgumentNullException("application");

CheckForConfigSettings();

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUhe);
_initialized = true;
}
}
}
}
protected virtual void OnUhe(object sender,
UnhandledExceptionEventArgs e)
{ // threaded exceptions occur on this method

// Let this occur one time for each AppDomain.
if (System.Threading.Interlocked.Exchange(ref
_unhandledExceptionCount, 1) != 0)
return;

Exception ex = e.ExceptionObject as Exception;

ProcessTheUHE(ex);
}

protected virtual void OnError(object sender, EventArgs args)
{
HttpApplication application = sender as HttpApplication;

Exception ex = application.Server.GetLastError() as Exception;

ProcessTheUHE(ex);
}
Jun 12 '07 #6
Thanks for your followup Chuck,

I think your current implementation is reasonable. BTW, for the
AppDomain.UnhandledException event, you only need to register the event
handler once since the AppDomain will remain running during ASP.NET Web
application's lifecycle.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
4
by: Bruce W.1 | last post by:
I need to spin of a thread that periodically checks the Application state. How can a thread access Application or Session states? Send it the httpContext? Thanks for your help.
5
by: Steve - DND | last post by:
If I declare a property as ThreadStatic, or I use the thread Context to set values on, will they be retained after a page processes? When a page finishes, is all data on the thread wiped before it...
7
by: Microsoft | last post by:
I'm trying to use threading for the first time and can't get it to work. (VB) In the page I've got: Sub Page_Load( sender As Object, e As EventArgs ) dim o as new Example.Test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.